How to stop UIView Animation when needed?

非 Y 不嫁゛ 提交于 2020-01-15 05:24:29

问题


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



来源:https://stackoverflow.com/questions/25848029/how-to-stop-uiview-animation-when-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!