How to stop UIView Animation when needed?

2019-07-16 14:11发布

问题:

I have this Code:

-(void)animationLoop{
CGPoint oldPoint = CGPointMake(myCircleUIView.frame.origin.x, myCircleUIView.frame.origin.y);
[UIView animateWithDuration:1.0
                 animations: ^{ myCircleUIView.frame = CGRectMake(myCircleUIView.frame.origin.x + [self randomFloatingGenerator], myCircleUIView.frame.origin.y + [self randomFloatingGenerator], myCircleUIView.frame.size.width, myCircleUIView.frame.size.height); }
                 completion:
 ^(BOOL finished) {
     [UIView animateWithDuration:1.0
                      animations:^{ myCircleUIView.frame = CGRectMake(oldPoint.x, oldPoint.y, myCircleUIView.frame.size.width, myCircleUIView.frame.size.height);}
                      completion:
      ^(BOOL finished) {[self animationLoop];}];
 }];
}

But I am trying to stop the animation when interacting with it, but [myCircleUIView.layer removeAllAnimations]; won't do the job, any suggestions?

回答1:

When you are stopping animation with CALayer's -removeAllAnimations completion callback is called with finished == NO. So change your animation code like this:

- (void)animationLoop {
    __weak id weakSelf = self;
    CGPoint oldPoint = CGPointMake(myCircleUIView.frame.origin.x, myCircleUIView.frame.origin.y);
    [UIView animateWithDuration:1.0
                     animations:^{
                         myCircleUIView.frame = CGRectMake(myCircleUIView.frame.origin.x + [weakSelf randomFloatingGenerator], myCircleUIView.frame.origin.y + [weakSelf randomFloatingGenerator], myCircleUIView.frame.size.width, myCircleUIView.frame.size.height);
                     }
                     completion:^(BOOL finished) {
                         if (!finished) return;
                         [UIView animateWithDuration:1.0
                                          animations:^{
                                              myCircleUIView.frame = CGRectMake(oldPoint.x, oldPoint.y, myCircleUIView.frame.size.width, myCircleUIView.frame.size.height);
                                          }
                                          completion:^(BOOL finished) {
                                              if (!finished) return;
                                              [weakSelf animationLoop];
                                          }];
                     }];
}

I also advise you not to pass strong references to self to blocks that are copied to heap if you don't really want to because of possible retain cycle.



回答2:

Add bool in your loop and set false when need to stop animation as show in code below...

BOOL IsAnimationContinue=true;

-(void)animationLoop{
CGPoint oldPoint = CGPointMake(myCircleUIView.frame.origin.x, myCircleUIView.frame.origin.y);
[UIView animateWithDuration:1.0
             animations: ^{
                              if(IsAnimationContinue)
                              {
                               myCircleUIView.frame = CGRectMake(myCircleUIView.frame.origin.x + [self randomFloatingGenerator], myCircleUIView.frame.origin.y + [self randomFloatingGenerator], myCircleUIView.frame.size.width, myCircleUIView.frame.size.height); 
                              }}
             completion:^(BOOL finished) {
 [UIView animateWithDuration:1.0
                  animations:^{ 
                                 if(IsAnimationContinue)
                                {
                                 myCircleUIView.frame = CGRectMake(oldPoint.x, oldPoint.y, myCircleUIView.frame.size.width, myCircleUIView.frame.size.height);
                                }}
                  completion:
  ^(BOOL finished) {[self animationLoop];}];

}]; }

Just set

IsAnimationContinue=False

To stop animation