码迷,mamicode.com
首页 > 其他好文 > 详细

定制转场动画ControllerTransitionAnimation

时间:2015-05-19 20:50:01      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

定制转场动画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 来实现特定动画的功能,便于解耦
  • 简单易懂

 

核心

技术分享

 

定制转场动画ControllerTransitionAnimation

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/4515196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!