您不能使用 CATransition 在两个视图控制器之间切换,但您可以简单地使用视图控制器的视图并与另一个视图控制器的视图进行切换。
iOS 5 引入了transitionFromViewController:toViewController:duration:options:animations:completion: 方法,使得使用 UIViewControllers 进行转换变得更加容易。
最后,您仍将使用 UIViewControllers 的 UIViews。
(实际上,您提供的链接正是您在两个 UIViewController 之间转换的方式。请记住,视图控制器仅管理视图而不是真正的视图本身。切换回来不会像 dismissModalViewControllerAnimated 那样容易:但有时自定义不是'不容易)。
以下是使用 iOS 5 方法的示例:
UIViewController *controller = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[controller.view setBackgroundColor:[UIColor greenColor]];
[self addChildViewController:controller];
[self transitionFromViewController:self.redController toViewController:controller duration:0.8 options:0 animations:^{
CATransition *transition = [CATransition animation];
transition.delegate = self;
transition.duration = 0.8;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
NSString *types[4] = {@"cube", @"rippleEffect", @"cube", @"alignedCube"};
NSString *subtypes[4] = {kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromRight};
transition.type = types[0];
transition.subtype = subtypes[1];
[self.view.layer addAnimation:transition forKey:nil];
} completion:^(BOOL finished) {
[controller didMoveToParentViewController:self];
}];
请注意,您必须事先执行以下操作。
[self addChildViewController:self.redController];
[self.view addSubview:self.redController.view];
在使用这些方法之前,您必须做一些事情。主要区别在于您将拥有比当前拥有的视图控制器多一个。这个额外的 UIViewController 充当其他两个的容器(有点像当前,self.view 是您拥有的两个 UIView 的容器)。