0

I have created slider in my app. How to add loop in my code. Because my label text slide only one time but i want to label text repeat (loop) on label. How it possible?

My Label slider code

Make globalCounter as global variable

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

[self changeLable];

}

Then

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

                 }];


}

Edit

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

                 }];


 }
rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

1

do like

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

}

and call method like

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




                 }];
}


}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
0

Try below code, it may help:

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

                 }];

}
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
0

This is my answer brother.Sorry I could not answer immediately as I had some work.I downloaded and ran your project.First it crashes as you handled the wrong array count.So it bounds the array.After that I set the condition inside the black.

-(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];
            }];
}
user3182143
  • 9,459
  • 3
  • 32
  • 39