重复UIAnimation块,并且一种方法再次停止它 [英] Repeating UIAnimation block, and a way to stop it again
问题描述
我想做一个小的加载动画放入我的应用程序。我已经在CGAnimations之前重复动画,没有问题,这次我要去块方法。
I wanted to do a small loader animation to put in my app. I have done repeating animations before with CGAnimations without problems, this time I was going for the block approach.
我做一个小测试,但可以使下面的代码重复:
I am doing a small test but can make the following code repeat:
- (void) startLoading {
__block int count = 0;
[UIView animateWithDuration:0.4
delay: 0.0
options: UIViewAnimationOptionRepeat
animations:^{
count++;
}
completion:^(BOOL finished){
if (count > 5)
count = 0;
NSLog(@"%d", count);
}];
}
- (void) stopLoading {
}
以上只会触发完成块一次,不会重复。
The above only fires the completion block once, it does not repeat.
重复,以便计数增量?
如果我得到这个工作,并把我的动画重复块,
什么进入stopLoading :要再次停止动画吗?
If I get this working and put my animation into the repeating block, What goes into stopLoading: to stop the animation again?
感谢任何帮助:)
推荐答案
这是一个有限重复的动画:
Here's a finite repeating animation:
- (void) animate: (int) count {
CGPoint origC = v.center;
void (^anim) (void) = ^{
v.center = CGPointMake(100,100);
};
void (^after) (BOOL) = ^(BOOL finished) {
v.center = origC;
if (count)
[self animate:count-1];
};
int opts = UIViewAnimationOptionAutoreverse;
[UIView animateWithDuration:1 delay:0 options:opts
animations:anim completion:after];
}
这里有一个递归,所以我们不想去将耗尽内存,但如果我们限制我们的计数(在你的例子是5),我们应该很好。
There's a recursion here, so we don't want to go overboard or we'll run out of memory, but if we limit our count (in your example it was 5) we should be fine.
这篇关于重复UIAnimation块,并且一种方法再次停止它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!