(1)项目中添加QuartzCore.framework组件
(2)在预编译文件中添加
#import<QuartzCore/QuartzCore.h>, 这样所有需要的文件都可以直接使用
(3)
- (IBAction) clickToSecond:(id) sender {
//加载将要切换的视图
UIViewControllerSecond * secondView = [[UIViewControllerSecond alloc] initWithNibName:@"UIViewControllerSecond"bundle:nil];
//传参数
secondView.view.frame =self.view.frame;
//设置为当前视图的同级视图
[self.viewaddSubview:secondView.view];
//定义动画类型
CATransition *animation = [CATransition animation];
animation.delegate =self;
animation.duration =1.25f;
animation.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fillMode =kCAFillModeForwards;
animation.type =kCATransitionFade;
animation.subtype =kCATransitionFromRight;
[self.view.layer addAnimation:animationforKey:@"animation"];
}
- (IBAction) clickReturnHome: (id) sender {
CATransition *animation = [CATransition animation];
animation.delegate =self;
animation.duration =0.50f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fillMode =kCAFillModeBoth;
animation.type =kCATransitionFade;
animation.subtype =kCATransitionFromRight;
[self.view.superview.layer addAnimation:animationforKey:@"animation"];
[self.viewremoveFromSuperview];
}
注意: 这2个切换视图之间的关系是父子视图关系。
切换显示还可以参考苹果实例: ViewTransitions, 其中切换视图是平级关系。