我有一个很好的动画。但是在动画结束时,动画停止了。我希望动画循环。我怎样才能做到这一点?这是我的代码:
- (void) startTicker
{
if (self.timerIsRunning) return;
self.timerIsRunning = YES;
NSTimer *newTimer = [NSTimer timerWithTimeInterval:(0.1) target:self selector:@selector(onTimer:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:newTimer forMode:NSDefaultRunLoopMode];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:100.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:0.0];
[UIView setAnimationDidStopSelector:@selector(moveToLeft:finished:context:)];
[UIView commitAnimations];
}
-(void)moveToLeft:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
imageView.left = 800;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:100.0];
[UIView setAnimationDelay:0.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:self cache:YES];
imageView.right = 800;
[UIView setAnimationDidStopSelector:@selector(moveToLeft2:finished2:context2:)];
[UIView commitAnimations];
}
答案 0 :(得分:1)
对于循环动画,最好的方法是使用块动画,它是UIView的一部分。
调用方法时:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
您可能想要的“选项”是 UIViewAnimationOptionAutoreverse 并与 UIViewAnimationOptionRepeat
结合使用例如:(循环闪烁红色边框)
[UIView animateWithDuration:2.0f delay:0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
[redBorder setAlpha:0]; //first part of animation
[redBorder setAlpha:0.5]; //second part of animation
} completion:nil];