Here is viewDidLoad from the main view controller in the test project:
- (void)viewDidLoad
{ [super viewDidLoad];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
[self.view addSubview:containerView];
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[redView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:redView];
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[yellowView setBackgroundColor:[UIColor yellowColor]];
[UIView transitionWithView:containerView duration:3
options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[redView removeFromSuperview];
[containerView addSubview:yellowView];
}
completion:NULL];
}
The yellow box just appears. There is no animation, no matter which UIViewAnimationOption I try. Why???
EDIT: I've also tried using performSelector withDelay to move the animation out of viewDidLoad and into another method. Same result - no animation.
Also tried this: [UIView transitionFromView:redView toView:yellowView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
Still, the yellow view just appears. No animation.
Don't put this in
viewDidLoad
.viewDidLoad
is called when the view is fully loaded into memory, but not yet displayed on screen.Try putting the above code in
viewDidAppear:
which is called when the view has appeared on screen.Edit: a side note, if performSelector:withDelay: fixes something, that means you're trying to do that thing wrong. You need to refactor somehow. performSelector:withDelay: is the wrong way to solve problems 99.999% of the time. As soon as you place this code on a different speed device (new iPhone, old iPhone) it will mess up.
After some testing it appears that you cannot create the container view and set up the animation within the same runloop and have things work. In order to make your code work, I first created the
containerView
and theredView
in theviewDidLoad
method. Then I put your animation into theviewDidAppear
method. So that I could reference thecontainerView
and theredView
from theviewDidAppear
method, I made them properties. Here is the code for anST_ViewController.m
file that will perform the animation you wanted.