iOS自定義轉場動畫(1)——自定義Push轉場動畫

版本:Xcode 7.0.1
語言:Objective-C

轉場動畫就是viewController之間切換的動畫。
主要有以下三種自定義方法:

  • 列Push & Pop
  • Modal
  • Segue

第一種是UINavigationController的轉場方法
第二種是模態(tài)跳轉
第三種是 Stroyboard 中的拖線,屬于 UIStoryboardSegue 類,通過繼承這個類來自定義轉場過程動畫。

先來看一下效果:


Push

首先說一下 Push 這種轉場的自定義,操作步驟如下:

準備工作

  1. 新建工程,刪掉Main storyboard,把程序設置的General的Main Interface清空。
  2. 新建一個類繼承UIViewController,在AppDelegate中創(chuàng)建一個UINavigationController,設置root為ViewController。
  3. 在ViewController創(chuàng)建一個ImageView,放在.h中
@property (nonatomic, retain) UIImageView * sourceImageView; // 源imageView,在fromVC中

給ImageView添加一張圖片
再創(chuàng)建一個按鈕,設置點擊按鈕進入SecondViewController。

  1. 在SecondViewController也創(chuàng)建一個ImageView
@property (nonatomic, retain) UIImageView * avatarImageView; // 替身imageView,在toVC中

開始動畫

  1. 創(chuàng)建一個文件繼承自 NSObject(我把它取名叫PushTransition), 并在<strong>.h</strong>文件中遵守UIViewControllerAnimatedTransitioning協(xié)議。
    由于Push和Pop是兩套動畫,所以需要兩個這樣的文件,判斷動畫類型的辦法在下面討論。
  2. 實現(xiàn)協(xié)議的兩個方法
// 指定動畫的持續(xù)時長
 1. (NSTimeInterval)transitionDuration;
// 轉場動畫的具體內容
 2. (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
  1. 在其中編寫 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中

  1. 在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;
    }
}

至此,push的轉場動畫就完成了

點擊此處下載源碼

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 概述 這篇文章,我將講述幾種轉場動畫的自定義方式,并且每種方式附上一個示例,畢竟代碼才是我們的語言,這樣比較容易上...
    伯恩的遺產閱讀 54,054評論 37 381
  • OC開發(fā)我們主要有以下三種自定義方法,供大家參考:Push & PopModalSegue 前兩種大家都很熟悉,第...
    ScaryMonsterLyn閱讀 1,684評論 1 3
  • 概述 這篇文章,我將講述幾種轉場動畫的自定義方式,并且每種方式附上一個示例,畢竟代碼才是我們的語言,這樣比較容易上...
    iOS_Developer閱讀 1,036評論 0 4
  • 準備: 蘋果在iOS7之后,提供了自定義轉場API。使得我們可以對模態(tài)(present、dismiss)、導航控制...
    M_慕宸閱讀 1,455評論 0 7
  • 寫出我心寫出我心寫出我心重復的事情說三遍,《寫出我心》這本書讓我養(yǎng)成了寫作的好習慣,在作者的鼓勵下,我成功的寫下了...
    南瓜橙長閱讀 299評論 0 0