1. 程式人生 > >三種ViewController跳轉的異同(兩種轉場動畫的實現待總結)

三種ViewController跳轉的異同(兩種轉場動畫的實現待總結)

三種viewctroller跳轉時的轉場動畫實現也不同。導航檢視控制器的為系統預設。present和addChildViewController的方式的轉場,前者可以用轉場動畫協議來實現,後者的轉場動畫可以用transitionFromViewController:toViewController:duration:options:animations:completion:來實現。這兩種轉場動畫的實現機制待總結。

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

MainVC *mainVC = [[MainVC alloc] init];

[self presentViewController:mainVC animated:YES completion:nil];

這種方式一般出現在需要使用者完成某件事情,如輸入密碼、增加資料等操作後,才能(回到跳轉前的控制器)繼續。例如系統的WIFI連線輸入密碼提示。預設動畫是從下至上。

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

這種方式一般是使用者瀏覽資料,繼而可以前進到下一個頁面或回到上一個頁面。預設動畫是從右至左。

- (void)addChildViewController:(UIViewController *)childController

這個方法出現在iOS5以後,通過它即使不使用NavigationController也能夠實現view hierarchy。有以下優點:

1.頁面邏輯很清晰,相應的View對應相應的ViewController。
2.當某個子View沒有顯示時,將不會被Load,減少了記憶體的使用。
3.當記憶體緊張時,沒有Load的View將被首先釋放,優化了程式的記憶體釋放機制。

複製程式碼
#import "ViewController.h"
#import "FirstVC.h
" #import "SecondVC.h" #import "ThirdVC.h" @interface ViewController () { FirstVC *firstVC; SecondVC *secondVC; ThirdVC *thirdVC; } @property (weak, nonatomic) IBOutlet UIView *contentView; @property (strong, nonatomic) UIViewController *currentVC; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. firstVC = [[FirstVC alloc] init]; secondVC = [[SecondVC alloc] init]; thirdVC = [[ThirdVC alloc] init]; [self addChildViewController:firstVC]; [self addChildViewController:secondVC]; [self addChildViewController:thirdVC]; [self.contentView addSubview:thirdVC.view]; self.currentVC = thirdVC; } - (IBAction)onClick:(id)sender { if(self.currentVC==firstVC && [sender tag]==1) { return; } if(self.currentVC==secondVC && [sender tag]==2) { return; } if(self.currentVC==thirdVC && [sender tag]==3) { return; } UIViewController *oldVC = self.currentVC; switch ([sender tag]) { case 1: { [self transitionFromViewController:self.currentVC toViewController:firstVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{ } completion:^(BOOL finished) { if(finished) { self.currentVC = firstVC; } else { self.currentVC = oldVC; } }]; } break; case 2: { [self transitionFromViewController:self.currentVC toViewController:secondVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{ } completion:^(BOOL finished) { if(finished) { self.currentVC = secondVC; } else { self.currentVC = oldVC; } }]; } break; case 3: { [self transitionFromViewController:self.currentVC toViewController:thirdVC duration:1 options:UIViewAnimationOptionTransitionCurlUp animations:^{ } completion:^(BOOL finished) { if(finished) { self.currentVC = thirdVC; } else { self.currentVC = oldVC; } }]; } break; default: break; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end