2016-09-07 83 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]; 
      }]; 
}