(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, 其中切換視圖是平級關係。