开发者

how to stop an already running animation

开发者 https://www.devze.com 2023-03-25 17:12 出处:网络
I am doing some animation using block level technique. I want to stop the animation on the basis of certain condition. How will I stop the animation from completing?

I am doing some animation using block level technique. I want to stop the animation on the basis of certain condition. How will I stop the animation from completing?

Following is my code for animation:

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{ 
                         if([arrAns count]>0)
                             vwb1.center = CGPointMake(260, 40);
                     } 
                     completion:^(BOOL finished){
                         [UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
                                          animations:^{ 
                                              if([arrAns count]>1)
                                                  vwb2.center = CGPointMake(260, 100);
                                          } 
                                          completion:^(BOOL finished){
                                              [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
                                                               animations:^{ 
                                                                   if([arrAns count]>2)
                                                                       vwb3.center = CGPointMake(260, 160);
                                                               } 
                                                               completion:^(BOOL finished){
                                                                   [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
                                                                                    animations:^{ 
                                                                                        if([arrAns count]>3)
                                                                                            vwb4.center = CGPointMake(260, 220);
                                                                                    } 
                                                                                    completion:^(BOOL finished){
                                                                                        [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ 
                                                                                            if([arrAns count]>4)
                                                                                                vwb5.center = CGPointMake(260, 280);
                                                                                        } completion:nil];
                        开发者_如何学C                                                            }];
                                                               }];

                                          }
                          ];

                     }];

I am not having any key here so I can nnot remove it on the basis of a key. There is also a feature as removeallanimation but I am not able to use it. If someone could help me on how to use that it will also be fine.

Edit: Ok I figured out how to do it, I am doing it using following code:

[self.view.layer removeAllAnimations];

But now I have problem that my block level animation which have not yet started is still running. So if I remove the animation at second block it still executes remaining block. I need to stop that also.

Thanks

Pankaj


You should check finished variable. And start next animation (inside a block) only if previous was finished (finished == YES), not stopped.

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

...

completion
A block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called. If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号