版本:Xcode 7.0.1
語言:Objective-C
轉場動畫就是viewController之間切換的動畫。
主要有以下三種自定義方法:
- 列Push & Pop
- Modal
- Segue
第一種是UINavigationController的轉場方法
第二種是模態(tài)跳轉
第三種是 Stroyboard 中的拖線,屬于 UIStoryboardSegue 類,通過繼承這個類來自定義轉場過程動畫。
先來看一下效果:
Push
首先說一下 Push 這種轉場的自定義,操作步驟如下:
準備工作
- 新建工程,刪掉Main storyboard,把程序設置的General的Main Interface清空。
- 新建一個類繼承UIViewController,在AppDelegate中創(chuàng)建一個UINavigationController,設置root為ViewController。
- 在ViewController創(chuàng)建一個ImageView,放在.h中
@property (nonatomic, retain) UIImageView * sourceImageView; // 源imageView,在fromVC中
給ImageView添加一張圖片
再創(chuàng)建一個按鈕,設置點擊按鈕進入SecondViewController。
- 在SecondViewController也創(chuàng)建一個ImageView
@property (nonatomic, retain) UIImageView * avatarImageView; // 替身imageView,在toVC中
開始動畫
- 創(chuàng)建一個文件繼承自 NSObject(我把它取名叫PushTransition), 并在<strong>.h</strong>文件中遵守UIViewControllerAnimatedTransitioning協(xié)議。
由于Push和Pop是兩套動畫,所以需要兩個這樣的文件,判斷動畫類型的辦法在下面討論。
- 實現(xiàn)協(xié)議的兩個方法
// 指定動畫的持續(xù)時長
1. (NSTimeInterval)transitionDuration;
// 轉場動畫的具體內容
2. (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
- 在其中編寫 Push 的動畫。具體的動畫實現(xiàn)過程都在代碼的注釋里:
// 指定動畫的持續(xù)時長
-(NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
return 0.5;
}
// 轉場動畫的具體內容
-(void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
// 獲取動畫的源控制器和目標控制器
ViewController * fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
SecondViewController * toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView * container = transitionContext.containerView;
// 創(chuàng)建一個imageView的截圖,并把原本imageView隱藏,造成以為移動的就是imageView的假象
UIView * snapshotView = [fromVC.sourceImageView snapshotViewAfterScreenUpdates:NO];
snapshotView.frame = [container convertRect:fromVC.sourceImageView.frame fromView:fromVC.view];
fromVC.sourceImageView.hidden = YES;
// 設置目標控制器的位置,并把透明度設為0,在后面的動畫中慢慢顯示出來變?yōu)?
toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
toVC.view.alpha = 0;
// 都添加到container中。注意順序
[container addSubview:toVC.view];
[container addSubview:snapshotView];
// 執(zhí)行動畫
[UIView animateKeyframesWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
snapshotView.frame = toVC.avatarImageView.frame;
toVC.view.alpha = 1;
} completion:^(BOOL finished) {
fromVC.sourceImageView.hidden = NO;
toVC.avatarImageView.image = fromVC.sourceImageView.image;
[snapshotView removeFromSuperview];
//一定要記得動畫完成后執(zhí)行此方法,讓系統(tǒng)管理 navigation
[transitionContext completeTransition:YES];
}];
}
使用動畫
回到ViewController中
- 在ViewController.h中遵守UINavigationControllerDelegate協(xié)議,并設置為self
// 必須在viewDidAppear或者viewWillAppear中寫,因為每次都需要將delegate設為當前界面
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.navigationController.delegate = self;
}
9、實現(xiàn) UINavigationControllerDelegate 協(xié)議方法:
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
if (operation == UINavigationControllerOperationPush){ // 就是在這里判斷是哪種動畫類型
return [[PushTransition alloc] init]; // 返回push動畫的類
}else{
return nil;
}
}