如何停止一个循环动画,并再次调用新的值



我目前有一个默认的循环动画,当视图出现时,它会逐渐淡出,当数据的后台处理完成时,它会停止当前动画并加载新的动画,就新图片而言

i cannot see to stop 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;
} 
[UIView animateWithDuration:4.0                  
    animations:^{                       
   imageViewTop.alpha = 0.0;                      
    imageViewBottom.alpha = 1.0;                      
    }                   
    completion:^(BOOL  completed){                      
    [self nextAnimation:stringsize.width];                  
    } 

这里,在你的完成块中,你将再次调用下一个动画方法,而不管动画是否被取消。

在完成块中,只在完成时调用nextAnimation == YES:

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

这个,再加上从视图中删除动画,实际上是由Till建议的,应该为你做。

你也可以创建一个timer对象,并通过该对象调用这个函数,一旦我们的后台处理结束invalidate对象[boothTimer1 invalidate]并再次调用。在我的一个应用程序中,我做了类似的场景你提到。在.h

中声明boothTimer1

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

现在你可以在后台处理完成时使定时器失效

最新更新