CATransition white flash in background?

2019-07-18 05:12发布

问题:

I'm trying to make a pushViewController slide in from the left, not the right, as follows:

Page14Controller * controller = [[Page14Controller alloc] initWithNibName:@"Page14Controller" bundle:nil];

CATransition* transition = [CATransition animation];
transition.duration = 0.3;
transition.type = kCATransitionPush;
//                transition.type = kCATransitionMoveIn; 
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
transition.subtype = kCATransitionFromLeft; 
//kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
[self.navigationController.view.layer addAnimation:transition forKey:nil];

[self.navigationController pushViewController:controller animated:NO];

When I do this, the current controller fades to white, which looks terrible. Why does this happen? I've tried setting the background of the current view's alpha to 0.

回答1:

I figured out what was apparently causing the white flash: it was the background of the main window. Setting this to alpha=0 helps the white effect. It appears that kCATransitionPush actually fades the view being pushed off screen, and obviously if what is under that is green, then it'll fade to green. It'd be nice if it didn't also fade, but that's a different question.



回答2:

I'd avoid messing with UIViewController's view too much.

The easier way to achieve what you want is with something like this:

UINavigationController * nc = self.navigationController;
NSArray * viewControllers = ;
NSMutableArray * newControllers = [NSMutableArray arrayWithArray:[nc viewControllers]];
[newControllers insertObject:controller atIndex:newControllers.count-1];
nc.viewControllers = newControllers;
[nc popViewControllerAnimated:YES];

Note that it replaces the top view controller with the new controller instead of just pushing it; this may not be what you want (but if you're using it to simulate page-changing, you probably don't want to perpetually push more controllers until you run out of memory).