Desmond
Desmond

Reputation: 5011

How to stop an looping animation and call it again with new value

i currently have a default looping animation which fade in and out once the view appear, when background processing of data are completed it will stop the current animation an load new animation, in terms of new picture.

i can't seen to stop the animation using [self.view.layer removeAllAnimations]; [self.imageViewBottom.layer removeAllAnimations]; [self.imageViewTop.layer removeAllAnimations];

-(void)nextAnimation:(float)previousWidth {

//picture loop
imageViewTop.image = imageViewBottom.image;
imageViewBottom.image = [imageArray objectAtIndex:[imageArray count] - 1];

[imageArray insertObject:imageViewBottom.image atIndex:0];
[imageArray removeLastObject];
imageViewTop.alpha = 1.0;
imageViewBottom.alpha = 0.0;

[UIView animateWithDuration:4.0
                 animations:^{ 
                     imageViewTop.alpha = 0.0;
                     imageViewBottom.alpha = 1.0;
                     } 
                 completion:^(BOOL  completed){
                     [self nextAnimation:stringsize.width];
                 }
 ]; 



-(void)foundSetup
{   
    //[imageViewTop removeFromSuperview];
    //[imageViewBottom removeFromSuperview];
    //[buttonCaption removeFromSuperview];
    [self.view.layer removeAllAnimations];
    [self.imageViewBottom.layer removeAllAnimations];
    [self.imageViewTop.layer removeAllAnimations];


    //[self.imageArray removeAllObjects];
    //self.imageArray = foundImageArray;
} 

Upvotes: 1

Views: 1728

Answers (2)

iamsult
iamsult

Reputation: 1579

You can go with timer as well create a timer object and call this function through that object once ur background processing gets over invalidate the object [boothTimer1 invalidate] and call again. In one of my application i have done for similar scenario u mentioned. Declare boothTimer1 in .h

NSTimer* boothTimer1; self.boothTimer1 = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(ur function) userInfo:nil repeats:NO];

Now u can invalidate the timer when ur background processing is completed.

Upvotes: 1

jrturton
jrturton

Reputation: 119292

[UIView animateWithDuration:4.0                  
    animations:^{                       
   imageViewTop.alpha = 0.0;                      
    imageViewBottom.alpha = 1.0;                      
    }                   
    completion:^(BOOL  completed){                      
    [self nextAnimation:stringsize.width];                  
    } 

Here, in your completion block, you will call the next animation method again regardless of the animation being cancelled or not.

Inside your completion block, only call nextAnimation if completed == YES:

completion:^(BOOL  completed){                      
if (completed)        
    [self nextAnimation:stringsize.width];                  
    }

This, coupled with removing animations from the views that are actually being animated as suggested by Till, should do it for you.

Upvotes: 3

Related Questions