我想做一个小的加载器动画来放入我的应用程序。我以前用 CGAnimations 做过重复动画没有问题,这次我要用 block 方法。
我正在做一个小测试,但可以重复以下代码:
- (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 {
}
上面只触发完成 block 一次,它不会重复。
如何让 block 重复以便计数递增?
如果我让它工作并将我的动画放入重复 block 中, 什么进入 stopLoading:再次停止动画?
感谢您提供的任何帮助:)
最佳答案
这是一个有限重复的动画:
- (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),我们应该没问题。
关于cocoa-touch - 重复 UIAnimation block ,以及再次停止它的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6616213/