Question

Look at this sample code (in AppDelegate):

- (void)showOtherView {
if (self.viewController.view == view1) {
    self.viewController.view = view2;
} else {
    self.viewController.view = view1;
}
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromRight];
[animation setDuration:0.5];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.window layer] addAnimation:animation forKey:@"myKey"];

}

This method simply switches view1 to view2. The thing I don't understand is, that CATransition is declared and added to window.layer after assigning view2 to self.viewController.view. Why does the animation works?

I did set breakpoints and figured out that switching animates after showOtherView method is finished. Why? Just why view2 doesn't instantly appear on the screen!? (at least for the first time I call this method)

Was it helpful?

Solution

Drawing to the screen is an expensive operation, and Cocoa only does it once per spin of the run loop, at the end. The effects of setting the controller's view -- it getting put on the screen -- don't actually happen until after this method has returned and control passes to the run loop. So whatever configurations you make to the new view will always be shown.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top