我想知道怎样才能把动画设置为重复。重复次数需要由一个变量来决定。在下面的代码中,变量int newPage应该确定动画重复的频率。
我尝试过这样做,但是动画(使用块动画)只执行了一次:
for (int temp = 1; temp <= newPage; temp++) {
[self animatePage];
}如果我编写了以下代码,它的工作方式就像我希望的那样,但是这是硬编码的(即动画将被重复两次),并且我无法找到一种方法来改变这个动画在代码中执行的次数,并根据我的变量newPage:
[UIView animateWithDuration:0
delay:0.1
options:UIViewAnimationOptionCurveEaseIn
animations:^{[self animatePage];}
completion:^(BOOL finished){[self animatePage];}];我将非常感激的建议,如何重复相同的动画,而不必硬编码的次数,我希望这个动画被重复。
编辑:
我试图实现以下代码,但实际上只执行了一个动画:
[UIView animateWithDuration:0
delay:1
options:UIViewAnimationOptionCurveEaseIn
animations:^{
[UIView setAnimationRepeatCount:2];
[self animatePage];
}
completion:nil];发布于 2013-03-16 22:34:13
也有同样的问题--你错过了一个‘UIView动画’
这应该是可行的:
[UIView animateWithDuration:0
delay:1
options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionRepeat
animations:^{
[UIView setAnimationRepeatCount:2]; // **This should appear in the beginning of the block**
[self animatePage];
}
completion:nil];为我做了个小把戏。
发布于 2011-04-28 18:15:17
你试过设置repeatCount吗?+ (void)setAnimationRepeatCount:(float)repeatCount
我尝试了下面的代码块,它肯定会为我重复2x (l是一个UITextView,在X dir中是2倍,在Y dir中是3X ):
[UIView animateWithDuration:2
delay:0.1
options:UIViewAnimationOptionCurveEaseIn
animations:^{ [UIView setAnimationRepeatCount:2];
l.transform = CGAffineTransformMakeScale(2,3); } completion:nil];发布于 2011-04-28 18:20:16
你之所以没有看到一个动画,是因为你是在同一个运行环中的一个循环中调用动画,在最后一个调用获胜时(一个动画)。
与其调用[self animatePage],不如尝试调用
[self performSelector:@selector(animatePage) withObject:nil afterDelay:.1 *temp];这将在单独的线程上创建调用。
您可能需要使用延迟间隔。
https://stackoverflow.com/questions/5823162
复制相似问题