如何连续回收UIView动画

lil*_*lzz 6 iphone animation

以下代码将图像视图从右向左移动一次,但我想继续进行.向右移动到左边然后再向左移动并再次从右向左重复.

imageview=[[UIImageView alloc] initWithFrame:CGRectMake(320, 200, 2635, 200)];

image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animal" ofType:@"png"]]; 
               [imageview setImage:image];

[self.view addSubview:imageview];

[UIView beginAnimations:@"MoveAndStrech" context:nil];
[UIView setAnimationDuration:40];     //30
[UIView setAnimationBeginsFromCurrentState:YES];
CGPoint p, b, a, c, d,e;
p.x = 0;
p.y = 200;

imageview.center=p;

[UIView commitAnimations];  
Run Code Online (Sandbox Code Playgroud)

Vla*_*mir 25

添加以下行:

[UIView setAnimationRepeatCount: HUGE_VAL];
[UIView setAnimationRepeatAutoreverses: YES];
Run Code Online (Sandbox Code Playgroud)

或者,如果您的目标是iOS 4.0+,则首选基于阻止的动画:

[UIView animateWithDuration:40.0f
                      delay:0.0f
                    options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
                 animations: ^(void){imageview.center = CGPointMake(0, 200);}
                 completion:NULL];
Run Code Online (Sandbox Code Playgroud)