如何停止循环动画并使用新值再次调用它

发布于 2024-12-19 18:56:49 字数 1317 浏览 5 评论 0原文

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

我看不到使用 [self.view.layer removeAllAnimations]; [self.imageViewBottom.layer removeAllAnimations]; [self.imageViewTop.层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;
} 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月如刀 2024-12-26 18:56:49
[UIView animateWithDuration:4.0                  
    animations:^{                       
   imageViewTop.alpha = 0.0;                      
    imageViewBottom.alpha = 1.0;                      
    }                   
    completion:^(BOOL  completed){                      
    [self nextAnimation:stringsize.width];                  
    } 

在这里,在完成块中,无论动画是否被取消,您都将再次调用下一个动画方法。

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

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

这,再加上从Till建议的实际正在动画化的视图中删除动画,应该可以为您完成。

[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.

三生路 2024-12-26 18:56:49

您也可以使用计时器创建一个计时器对象,并在后台处理结束后通过该对象调用此函数,使对象无效 [boothTimer1 invalidate] 并再次调用。在我的一个应用程序中,我已经针对您提到的类似场景进行了操作。在.h

NSTimer*中声明boothTimer1; self.boothTimer1 = [NSTimer ScheduledTimerWithTimeInterval:持续时间目标:self 选择器:@selector(ur 函数) userInfo:nil 重复:NO];

现在,当后台处理完成时,您可以使计时器无效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文