iOS ~ ViewController的Push,Pop和Present,Dismiss轉場動畫

SSBun發表於2017-12-13

轉場動畫涉及到的包括導航控制器的Push動畫和Pop動畫,以及普通控制器的Present和Dismiss動畫,主要就是通過控制器遵守UIViewControllerTransitioningDelegate,並實現對應的方法,返回一個遵守UIViewControllerAnimatedTransitioning協議的物件,而主要的動畫實現就是寫在這個物件當中~

  • __ 如果是Push和pop動畫需要再push控制器和pop時的控制器裡同時實現導航控制器的代理以實現Push和Pop的動畫,而Present和Dismiss動畫只需要在Presnent的時候設定代理,並實現present和dismiss的協議方法就OK了 __
  • __ 為了方便使用,一般會封裝代理返回的動畫協議物件(UIViewControllerAnimatedTransitioning),通過之類來進一步處理各種各樣的轉場動畫 __
//
//  ViewController.m
//  transitionDemo
//  Copyright © 2016年 SSBun. All rights reserved.
//

#import "ViewController.h"
#import "TwoViewController.h"
#import "PushAnimator.h"
#import "PresentAnimator.h"
#import "DismissAnimator.h"

@interface ViewController ()<UIViewControllerTransitioningDelegate,UINavigationControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    TwoViewController *twoVc = [[TwoViewController alloc] init];
    // Present
    twoVc.transitioningDelegate  = self;
    twoVc.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:twoVc
                       animated:YES
                     completion:^{
                     }];
    // Push
    // [self.navigationController pushViewController:twoVc animated:YES];
}

#pragma mark - 動畫代理
- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC{
    
    if ([toVC isKindOfClass:[TwoViewController class]]) {
        
        PushAnimator *transition = [[PushAnimator alloc] init];
        return transition;
        
    }else{
        
        return nil;
    }
}

#pragma mark - 定製轉場動畫 (Present 與 Dismiss動畫代理)
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source {
    
    // 推出控制器的動畫
    return [PresentAnimator new];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    
    DismissAnimator *dismissAnimator   = [DismissAnimator new];
    dismissAnimator.transitionDuration = 1.f;
    
    // 退出控制器動畫
    return dismissAnimator;
}

@end
複製程式碼

__ 可以看出協議方法主要時返回了一個遵守協議的物件,所以主要的重點在實現這個物件上 __

//  BaseAnimator.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface BaseAnimator : NSObject <UIViewControllerAnimatedTransitioning>

/**
 *  動畫執行時間(預設值為0.5s)
 */
@property (nonatomic) NSTimeInterval  transitionDuration;

/**
 *  == 子類重寫此方法實現動畫效果 ==
 *
 *  動畫事件
 */
- (void)animateTransitionEvent;

/**
 *  == 在animateTransitionEvent使用才有效 ==
 *
 *  源頭控制器
 */
@property (nonatomic, readonly, weak) UIViewController *fromViewController;

/**
 *  == 在animateTransitionEvent使用才有效 ==
 *
 *  目標控制器
 */
@property (nonatomic, readonly, weak) UIViewController *toViewController;

/**
 *  == 在animateTransitionEvent使用才有效 ==
 *
 *  containerView
 */
@property (nonatomic, readonly, weak) UIView           *containerView;

/**
 *  動畫事件結束
 */
- (void)completeTransition;

@end
複製程式碼
//  BaseAnimator.m
#import "BaseAnimator.h"

@interface BaseAnimator ()

@property (nonatomic, weak) id <UIViewControllerContextTransitioning> transitionContext;

@property (nonatomic, weak) UIViewController  *fromViewController;
@property (nonatomic, weak) UIViewController  *toViewController;
@property (nonatomic, weak) UIView            *containerView;

@end

@implementation BaseAnimator

#pragma mark - 初始化
- (instancetype)init {
    
    self = [super init];
    if (self) {
    
        // 預設引數設定
        [self deafultSet];
    }
    
    return self;
}

- (void)deafultSet {
    
    _transitionDuration = 0.5f;
}

#pragma mark - 動畫代理
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    return _transitionDuration;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    
    self.fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    self.toViewController   = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    self.containerView      = [transitionContext containerView];
    self.transitionContext  = transitionContext;
    
    [self animateTransitionEvent];
}

- (void)animateTransitionEvent {
    
    /* == 程式碼示例 ==
     
    UIView *tmpView  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [self.containerView addSubview:self.toViewController.view];
    [self.containerView addSubview:tmpView];
    
    [UIView animateWithDuration:self.transitionDuration
                          delay:0.0f
         usingSpringWithDamping:1 initialSpringVelocity:0.f options:0 animations:^{
             
             tmpView.frame = CGRectMake(0, 0, 100, 100);
             
         } completion:^(BOOL finished) {
             
             [tmpView removeFromSuperview];
             [self completeTransition];
         }];
     */
}

#pragma mark -
- (void)completeTransition {

    [self.transitionContext completeTransition:!self.transitionContext.transitionWasCancelled];
}

@end
複製程式碼

__ 其中的fromViewController就是準備跳轉的控制器,toViewController 就是要跳轉到的控制器,containerView是整個動畫的畫布,需要將要跳轉到的控制器的view新增到畫布上執行動畫,而在animateTransitionEvent中就時具體實現動畫的過程了,不要忘了還要返回動畫時間,用起來的時候就看各自的發揮了,可以編寫子類來實現這個方法~ __

相關文章