如何让处理程序重复 UIView animateWithDuration? [英] How to have a handler to repeat UIView animateWithDuration?
问题描述
我正在使用 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.
推荐答案
假设您已经创建了一个 canceled 属性,您可以执行类似的操作.如评论中所述,完成块的 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!