Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do a continuous recycling UIView animation

The following code move the the imageview from right to left once, but I want to do continously. move right to to left then move back to left offscreen and repeat right to left again.

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];  
like image 496
lilzz Avatar asked Apr 16 '11 22:04

lilzz


1 Answers

Add the following lines:

[UIView setAnimationRepeatCount: HUGE_VAL];
[UIView setAnimationRepeatAutoreverses: YES];

Or if you target iOS 4.0+ then blocked-based animations are preferred:

[UIView animateWithDuration:40.0f
                      delay:0.0f
                    options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
                 animations: ^(void){imageview.center = CGPointMake(0, 200);}
                 completion:NULL];
like image 183
Vladimir Avatar answered Nov 15 '22 08:11

Vladimir