2016-09-07 80 views
0

我在我的應用程序中創建了滑塊。如何在我的代碼中添加循環。因爲我的標籤文本只滑動一次,但我想在標籤上標註文本重複(循環)。怎麼可能?如何創建標籤文本滑塊

我的標籤滑塊代碼

讓globalCounter爲全局變量

globalCounter=0; 
if(nameArray.count>0){ 

[self changeLable]; 

} 

然後

-(void)changeLable{ 

if(!(globalCounter<nameArray.count)){ 
    return; 
} 


NSLog(@"globalCounter %d",globalCounter); 

[UIView animateWithDuration:1 
         delay:0.5 
        options: UIViewAnimationOptionTransitionCrossDissolve 
       animations:^{ 


       } 
       completion:^(BOOL finished) { 

        [lblTitle setText:[nameArray objectAtIndex:globalCounter]]; 
        globalCounter++; 

        [self performSelector:@selector(changeLable) withObject:nil afterDelay:1]; 

       }]; 


} 

編輯

-(void)changeLable{ 

if(!(globalCounter<nameArray.count)){ 
globalCounter=0; 
    } 


NSLog(@"globalCounter %d",globalCounter); 

[UIView animateWithDuration:1 
         delay:0.5 
        options: UIViewAnimationOptionTransitionCrossDissolve 
       animations:^{ 


       } 
       completion:^(BOOL finished) { 

        [lblTitle setText:[nameArray objectAtIndex:globalCounter]]; 
        globalCounter++; 

        [self performSelector:@selector(changeLable) withObject:nil afterDelay:1]; 

       }]; 


} 
+0

使用的Web視圖,而不是標籤和使用HTML語法 –

+0

適用於您的文字跑馬燈效果使用[MarqueeLabel(https://github.com/cbpowell/MarqueeLabel) – pkc456

+0

http://stackoverflow.com/questions/22397122 /如何移動文本從右到左 - in-ios-programatically/22397613#22397613 –

回答

1

不喜歡

globalCounter=0; 
if(des.count>0){ 
    [_label setText:[des objectAtIndex:globalCounter]]; 
    [self changeLable]; 

} 

,並呼籲像

-(void)changeLable{ 

if(globalCounter < des.count){ 



[UIView animateWithDuration:0. 
         delay:0.5 
        options: UIViewAnimationOptionTransitionCrossDissolve 
       animations:^{ 


       } 
       completion:^(BOOL finished) { 

        if(globalCounter>=des.count){ 
         globalCounter=0;//set counter to zero after it exceeds array count to repeat text change round repeated 
        }else 
        { 
          [_label setText:[des objectAtIndex:globalCounter]]; 
         globalCounter++; 
         [self performSelector:@selector(changeLable) withObject:nil afterDelay:1]; 
        } 




       }]; 
} 


} 
+0

檢查更新的答案 –

+0

你的問題得到解決或不 –

0

嘗試下面的代碼,它可以幫助:

-(void)changeLable{ 

NSLog(@"globalCounter %d",globalCounter); 

[UIView animateWithDuration:1 
         delay:0.5 
        options: UIViewAnimationOptionTransitionCrossDissolve 
       animations:^{ 

        [lblTitle setText:[nameArray objectAtIndex:globalCounter]]; 
       } 
       completion:^(BOOL finished) { 

        if(globalCounter>nameArray.count)){ 

         globalCounter=0;//set counter to zero after it exceeds array count to repeat text change round repeated 

        }else{ 

         globalCounter++; 

        } 

        [self performSelector:@selector(changeLable) withObject:nil afterDelay:1]; 

       }]; 

} 
+0

我使用您的代碼,但數組第一輪後崩潰我的應用程序錯誤顯示如下: - - [__ NSArrayI objectAtIndex:]:索引2超越邊界[0 .. 1]' –

+0

Error.I現在嘗試了您的代碼,它崩潰了。由於uncau ght異常'NSRangeException',原因:'*** - [__ NSArrayM objectAtIndex:]:index 4 beyond bounds [0 .. 3]' – user3182143

+0

'您的代碼工作正常man.it調用所有數組對象。 – user3182143

0

方法這就是我的回答brother.Sorry我不能馬上回答,因爲我有一些幹活下載並運行你的project.First首先崩潰,因爲你處理錯誤的數組count.So它綁定數組。然後,我設置條件內的黑色。

-(void)changeLable 
{ 

    NSLog(@"globalCounter %d",globalCounter); 

    [UIView animateWithDuration:1 
         delay:0.5 
        options: UIViewAnimationOptionTransitionCrossDissolve 
       animations:^{ 

       } 
       completion:^(BOOL finished) 
       { 
        if(globalCounter<des.count) 
        { 
         _label.text = @""; 
         [_label setText:[des objectAtIndex:globalCounter]]; 
         globalCounter++; 
        } 
        else 
         globalCounter=0; 
        [self performSelector:@selector(changeLable) withObject:nil afterDelay:1]; 
      }]; 
}