Reputation: 4127
I have two UIViews that I want to move at the same speed and at the same time.
[UIView animateWithDuration:0.25 animations:^ {
myView1.frame = CGRectMake(0.0, 0.0, 100.0, myView1.frame.size.height - 50.0);
myView2.frame = CGRectMake(0.0, 0.0, 100.0, myView1.frame.size.height - 100.0);
}];
However, doing the above means myView2
animates at a speed that is twice as fast as myView1
.
How can I get both views to animate at the same but at the same speed (so that one finishes first, followed by the other, but they start animating at the same time)?
Upvotes: 2
Views: 1190
Reputation: 62062
Start them at the same time and give the animation that needs to travel twice as far twice as much time to do so:
[UIView animateWithDuration:0.25 animations:^ {
myView1.frame = CGRectMake(0.0, 0.0, 100.0, myView1.frame.size.height - 100.0);
}];
[UIView animateWithDuration:0.5 animations:^ {
myView2.frame = CGRectMake(0.0, 0.0, 100.0, myView1.frame.size.height - 50.0);
}];
Basic velocity math.
An alternate approach:
[UIView animateWithDuration:0.25 animations:^{
myView1.frame = CGRectMake(0.0, 0.0, 100.0, myView1.frame.size.height - 50.0);
myView2.frame = CGRectMake(0.0, 0.0, 100.0, myView1.frame.size.height - 50.0);
}
completion:^{
[UIView animateWithDuration:0.25 animations:^{
myView2.frame = CGRectMake(0.0, 0.0, 100.0, myView1.frame.size.height - 50.0);
}];
}];
I'm not sure which method is better or would be preferred.
The first approach uses two separate calls to animateWithDuration
, one for each view, with one animating twice the distance in twice the time (so the same velocity overall).
The second approach uses a nested call to animateWithDuration
via a completion
block. First, both views are animated to the same size at the same speed. Next, the view that needs to go twice the distance is animated for the remaining distance (at the same velocity).
Upvotes: 5