本文介绍了在需要的时候如何停止的UIView动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的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];}];
}];
}
但我想阻止它进行交互时,但动画 [myCircleUIView.layer removeAllAnimations];
不会做的工作,有什么建议
But I am trying to stop the animation when interacting with it, but [myCircleUIView.layer removeAllAnimations];
won't do the job, any suggestions?
推荐答案
当你停止动画与的CALayer的
-removeAllAnimations
完成回调调用与完成== NO
。因此,改变动画code是这样的:
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
.
这篇关于在需要的时候如何停止的UIView动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!