I am trying to create an animation in my iOS application where the new image will slide on the screen on the left, while the main image will slide to the right to make room for it. The main view is a subclass of my top view controller view, and the side view that is moving is loaded from a separate xib file.
In case that matters, here is my code to load the side view, called from my viewDidLoad method of the main view controller:
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 is the code that is called for the animation of two kinds:
sideView.hidden = NO; [UIView animateWithDuration:0.25f animations:^{ mainView.frame = CGRectMake(200, 0, 320, 460); sideView.frame = CGRectMake(0, 0, 200, 460); }];
It seems simple enough. But for some reason, only sideView animates - mainView is not moving anywhere. And to make things even more confusing, if I comment out a line in the animation block that moves the sideView, the animation for mainView starts working.
Does anyone know what happened? Of all the searches and documents that I read, I should work fine. What I thought would be simple animation turned into hours of disappointment. Any help would be greatly appreciated!
Edit:
Turning Off Guo Luchuan Offers I tried to animate the various properties of the two UIViews. Most got the same result, although the animation of both of their transform properties almost worked. In this case, both UIViews are animated, but mainView just did wrong: its animation started from it in a different position and ended in the wrong place. It seems that he is doing the translation that I entrusted him with, but starting from this position, expressed in vector calculations:
starting_point - 0.5 * total_translation
means that it ends with:
starting_point + 0.5 * total_translation
The side view, however, is animated correctly.
This is damn annoying. I did not understand that the animation on iOS in this way was so broken. The next thing I'm going to try is to use CABasicAnimation, although I'm not happy that I have to resort to using such a low-level API for something so simple.