定製轉場動畫ControllerTransitionAnimation

weixin_33766168發表於2016-04-15

定製轉場動畫ControllerTransitionAnimation

 

說明

控制器轉場動畫的實現晦澀難懂,本人僅在這裡實現了非實時(不支援邊緣拖拽手勢)的轉場動畫效果,支援實時轉換的轉場動畫還在研究當中.

 

效果

 

原始碼

https://github.com/YouXianMing/ControllerTransitionAnimation

//
//  VirtualAnimator.h
//  Transition
//
//  Created by YouXianMing on 15/5/19.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

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

@interface VirtualAnimator : NSObject <UIViewControllerAnimatedTransitioning>

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

/**
 *  <建議此方法用子類過載實現來設定時間>
 *
 *  設定轉場動畫持續時間
 *
 *  @param transitionContext 操作控制程式碼
 *
 *  @return 設定的時間
 */
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext;

/**
 *  <此方法必須由子類過載實現>
 *
 *  轉場動畫實現
 *
 *  @param transitionContext 操作控制程式碼
 */
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;

/**
 *  獲取fromViewController的view
 *
 *  @param transitionContext 操作控制程式碼
 *
 *  @return fromViewController的view
 */
- (UIView *)currentViewWith:(id <UIViewControllerContextTransitioning>)transitionContext;

/**
 *  獲取toViewController的view
 *
 *  @param transitionContext 操作控制程式碼
 *
 *  @return toViewController的view
 */
- (UIView *)toViewWith:(id <UIViewControllerContextTransitioning>)transitionContext;

/**
 *  獲取fromViewController
 *
 *  @param transitionContext 操作控制程式碼
 *
 *  @return fromViewController
 */
- (UIViewController *)currentViewControllerWith:(id <UIViewControllerContextTransitioning>)transitionContext;

/**
 *  獲取toViewController
 *
 *  @param transitionContext 操作控制程式碼
 *
 *  @return toViewController
 */
- (UIViewController *)toViewControllerWith:(id <UIViewControllerContextTransitioning>)transitionContext;

/**
 *  轉場動畫完成
 */
- (void)transitionCompleteWith:(id <UIViewControllerContextTransitioning>)transitionContext;

@end


//
//  VirtualAnimator.m
//  Transition
//
//  Created by YouXianMing on 15/5/19.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "VirtualAnimator.h"

@implementation VirtualAnimator

- (instancetype)init {
    self = [super init];
    if (self) {

        // 預設引數設定
        [self deafultSet];
    }
    return self;
}

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

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    return _transitionDuration;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {

}

- (UIView *)currentViewWith:(id <UIViewControllerContextTransitioning>)transitionContext {
    return [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
}

- (UIView *)toViewWith:(id <UIViewControllerContextTransitioning>)transitionContext {
    return [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
}

- (UIViewController *)currentViewControllerWith:(id <UIViewControllerContextTransitioning>)transitionContext {
    return [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
}

- (UIViewController *)toViewControllerWith:(id <UIViewControllerContextTransitioning>)transitionContext {
    return [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
}

- (void)transitionCompleteWith:(id <UIViewControllerContextTransitioning>)transitionContext {
    [transitionContext completeTransition:YES];
}

@end

特點

  • 分離出 present 與 dissmiss 的動畫效果
  • 繼承抽象父類 VirtualAnimator 來實現特定動畫的功能,便於解耦
  • 簡單易懂

 

核心

相關文章