Animating multiple UIViews simultaneously

I have three UIButton objects visually stacked on top of each other. When the user presses the button, the buttons below should move a certain distance. I use the following animation block:

 // Assuming button 1 was clicked... [UIView animateWithDuration:0.25f delay:0.0f options:UIViewAnimationOptionCurveLinear animations:^(void) { self.button2.frame = CGRectOffset(self.button2.frame, 0.0f, 20.0f); self.button3.frame = CGRectOffset(self.button3.frame, 0.0f, 20.0f); } completion:^(BOOL finished) { NSLog(@"Finished"); }]; 

If I increase the duration of the animation, for example. from 0.25 to 0.75 , the buttons do not stay together, but they begin to move at different time intervals. I tried using the methods of Core Animation, grouping animations and other materials, but have not yet found a solution.

Do you have any ideas? Now I keep the duration at 0.25 until I come up with something.

+4
source share
1 answer

One solution is to set both button2 and button3 as a preview in another UIView and animate the presentation instead of each of the buttons separately. Whether this is a good approach depends on what you are trying to accomplish with a complex button.

EDIT:

Since my experience is that the animations inside the block are synchronized, I implemented the code as shown below. I tried numerous values ​​for the duration of the animation (0.15, 0.25, 0.75, 1.25) and button2 and Button3 move synchronously (and since button2 is on top of Button3, I cannot actually see Button3 at all until I click on button2, which makes button 3 move out from under button 3).

 - (IBAction)button1Tapped:(id)sender { NSLog(@"button1Tapped..."); [UIView animateWithDuration:0.75f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void) { self.button2.frame = CGRectOffset(self.button2.frame, 0.0f, 20.0f); self.button3.frame = CGRectOffset(self.button3.frame, 0.0f, 20.0f); } completion:^(BOOL finished) { NSLog(@"Finished"); }]; } - (IBAction)button2Tapped:(id)sender { NSLog(@"button2Tapped..."); [UIView animateWithDuration:0.75f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void) { self.button3.frame = CGRectOffset(self.button3.frame, 0.0f, 20.0f); } completion:^(BOOL finished) { NSLog(@"Finished"); }]; } - (IBAction)button3Tapped:(id)sender { NSLog(@"button3Tapped..."); } 
+7
source

All Articles