I'm trying to create an animation in my iOS app where a new view slides on screen from the left while the main view slides to the right to make room for it. The main view is a subview of my view controller's top view, and the side view that slides in is loaded from a separate xib file.
In case it's relevant, here's my code for loading the side view, called from my main view controller's viewDidLoad method:
sideViewController = [[SideViewController alloc] init];
[sideViewController loadView];
sideView = sideViewController.topView;
[self.view addSubview:sideView];
sideView.hidden = YES;
sideView.frame = CGRectMake(-200, 0, 200, 460);
And here's the code that gets called to animate the two views:
sideView.hidden = NO;
[UIView animateWithDuration:0.25f
animations:^{
mainView.frame = CGRectMake(200, 0, 320, 460);
sideView.frame = CGRectMake(0, 0, 200, 460);
}];
This seems simple enough. But for some reason, only sideView animates -- mainView doesn't move anywhere. And to make things even more confusing, if I comment out the line in the animation block that moves sideView, the animation for mainView starts working.
Does anyone know what's wrong? From all of the searches and docs I've read, what I'm doing ought to work fine. What I thought was going to be a simple animation has turned into hours of frustration. Any help would be much appreciated!
Edit:
Going off of Guo Luchuan's suggestion I tried animating different properties of the two UIViews. Most got the same result, though animating both of their transform properties almost worked. In that case both UIViews animated, but the mainView just did the wrong thing: its animation started with it in a different position and ended in the wrong place. It looks like it's performing the translation I instructed it to do, but starting at this position expressed in vector calculations:
starting_point - 0.5 * total_translation
meaning it ends at:
starting_point + 0.5 * total_translation
The sideView, however, animates correctly.
This is damned annoying. I didn't realize the performing animations on iOS in this way was so broken. The next thing I'm going to try is using CABasicAnimation, though I'm unhappy I have to resort to trying such a low level API for something so simple.
I have met a very same problem like you .
When I set 2 views same property (like frame
or transform
) at the same time , in the UIView animation block
, it will not work for what I expect.
So , I set a views frame
and set another views transform
, then it works well , but I do not know why it will be that.
I think it will works fine for you.
sideView.hidden = NO;
[UIView animateWithDuration:0.25f
animations:^{
mainView.frame = CGRectMake(200, 0, 320, 460);
sideView.transform = CGAffineTransformMakeTranslation(200,0);
}];
And I think you can make 2 CABasicAnimation
and then add them into the CAAnimationGroup
. It also can work for you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With