问题描述
我使用UIView类的方法animateWithDuration为重复我的看法动画。我怎么能有可用于以后停止这个动画的处理程序?例如,重复动画开始在一个方法,我需要从其他方法后停药。
I'm using UIView class method animateWithDuration for repeating my view animation. How can I have a handler that could be used to stop this animation later? For example, repeated animation starts in one method and I need to stop it later from another method.
推荐答案
您可以做这样的事情假设你已经创建了一个的取消的属性。正如在评论中指出,完成模块的startAnimation调用需要被包裹在一个异步调用,以避免堆栈溢出。一定要与任何类类型你确实有取代ID。
You could do something like this assuming you have created a canceled property. As noted in the comments the completion block's startAnimation call needs to be wrapped in an async call to avoid a stack overflow. Be sure to replace the "id" with whatever class type you actually have.
- (void)startAnimation {
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void) {
//animate
}
completion:^(BOOL finished) {
if(!self.canceled) {
__weak id weakSelf = self;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[weakSelf startAnimation];
}];
}
}
];
}
这篇关于如何拥有一个处理程序,重复的UIView animateWithDuration?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!