UIViewWithView transition does not work

Here's the 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]; } 

Only a yellow frame will appear. There is no animation, whatever the UIViewAnimationOption is. Why???

EDIT: I also tried using performSelector withDelay to move the animation from viewDidLoad to another method. The same result - no animation.

Also tried the following: [UIView transitionFromView: redView toView: yellowView duration: 3 options: UIViewAnimationOptionTransitionFlipFromLeft: NULL];

However, only a yellow appearance appears. No animation.

+7
source share
2 answers

After some testing, it seems that you cannot create a container view and set up the animation within the same runloop, and everything will work. To make your code work, I first created containerView and redView in the viewDidLoad method. Then I put your animation in the viewDidAppear method. So that I can refer to containerView and redView on the viewDidAppear method, I made them properties. Here is the code for the ST_ViewController.m file that will perform the required animation.

 @interface ST_ViewController () @property (nonatomic, strong) UIView *containerView; @property (nonatomic, strong) UIView *redView; @end @implementation ST_ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setContainerView:[[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)]]; [[self view] addSubview:[self containerView]]; [self setRedView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]]; [[self redView] setBackgroundColor:[UIColor redColor]]; [[self containerView] addSubview:[self redView]]; } -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; [yellowView setBackgroundColor:[UIColor yellowColor]]; [UIView transitionWithView:[self containerView] duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^(void){ [[self redView] removeFromSuperview]; [[self containerView] addSubview:yellowView]; } completion:nil]; } @end 
+6
source

Do not put this in viewDidLoad . viewDidLoad is called when the view is fully loaded into memory, but not yet displayed on the screen.

Try putting the above code in viewDidAppear: which is called when the view appears on the screen.

Edit: side of the note, if performSelector: withDelay: fixes something, it means that you are trying to do it wrong. You need to reorganize somehow. performSelector: withDelay: this is the wrong way to solve problems 99.999% of the time. As soon as you place this code on another speed device (new iPhone, old iPhone), it will go bad.

+1
source

All Articles