UIView block animation move view left to right and back to left

the Reverend picture the Reverend · Dec 30, 2011 · Viewed 28.7k times · Source

I wan't to animate a view that's shaped as an arrow, and I want it to animate from left to right, left to right and so on..

This code below is not working, I'm guessing I need a path, but don't know how to do that in a UIView block animation.

   [UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionRepeat animations:^{
    controller.view.frame = frame1;
    controller.view.frame = frame2;

} completion:^(BOOL finished) {

}];

Answer

Kjuly picture Kjuly · Dec 30, 2011

You can use UIViewAnimationOptionAutoreverse option, try the code below:

UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(20.0f, 100.0f, 300.0f, 200.0f)];
[testView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:testView];

[UIView animateWithDuration:0.3f
                      delay:0.0f
                    options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                 animations:^{
                     [testView setFrame:CGRectMake(0.0f, 100.0f, 300.0f, 200.0f)];
                 }
                 completion:nil];

[testView release];

It'll repeat move from right to left, left to right, ... smoothly.