如何创建标签文本滑块

时间:2016-09-07 09:19:21

标签: ios objective-c uilabel

我在我的应用中创建了滑块。如何在我的代码中添加循环。因为我的标签文本只滑了一次,但我想在标签上标记文本重复(循环)。怎么可能?

我的标签滑块代码

将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];

                 }];


 }

3 个答案:

答案 0 :(得分: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];
                     }




                 }];
}


}

答案 1 :(得分: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];

                 }];

}

答案 2 :(得分:0)

这是我的答案兄弟。我无法立即回答,因为我有一些工作。我下载并运行你的项目。首先它崩溃,因为你处理错误的数组计数。所以它绑定数组。之后我设置了黑色里面的条件。

-(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];
            }];
}