14

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) {

}];
Charles
  • 50,943
  • 13
  • 104
  • 142
the Reverend
  • 12,305
  • 10
  • 66
  • 121

2 Answers2

34

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.

Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • your animation works well when `TestView` is added on `ViewController`...but not when it is added on a another `View`..if possible plz help...http://stackoverflow.com/questions/16148239/animating-subviews-left-right-left – Subbu Apr 23 '13 at 05:57
  • Hi @CodeJack , it does work even on another View, what you have to modify is only to replace `[self.view addSubview:testView];` to `[self addSubview:testView];`. That's all. I've tested it & it works perfect. ;) – Kjuly Apr 23 '13 at 11:36
  • @Kjuly My UIView have three UIButton. This button not remove while animate view. It's still there. How to move all subview while UIView animate – Thangavel Jan 18 '14 at 07:25
  • @Thangavel just add your buttons on the view as subviews. :) For the answer as an e.g., add these buttons on `testView`. – Kjuly Jan 18 '14 at 08:20
  • @Kjuly I added button is subview to my view. My animate view is top to buttom. I set frame to UIView is [_dropLocationView setFrame:CGRectMake(0.0f, 140.0f, 320.0f, 0.0f)]; and set frame to [_dropLocationView setFrame:CGRectMake(0.0f, 140.0f, 320, 120.0f)]; while animate. Button is not moved with view while animate. – Thangavel Jan 18 '14 at 09:00
  • @Thangavel the params you offered means to animate the view height, not the position. What you need to do is change the value of origin.y. Try from `(0.0f, 140.0f, 320.0f, 120.0f)` to `(0.0f, 0.0f, 320.0f, 120.0f)`. I suggest you to take a look at the doc of `CGRectMake`. :) – Kjuly Jan 18 '14 at 14:42
0

Assuming that frame1 is the starting position and frame2 is the end position before repeat.

The problem is that the animation is calculated at the end of the runloop therefore the frame1 is ignored and the view is simply moved to frame2. If you move the setting of frame1 outside of the block then it should work fine. If you want the animation to play back and forward you will need to make it autoreverse with UIViewAnimationOptionAutoreverse

Paul.s
  • 38,494
  • 5
  • 70
  • 88