说明
控制器转场动画的实现晦涩难懂,本人仅在这里实现了非实时(不支持边缘拖拽手势)的转场动画效果,支持实时转换的转场动画还在研究当中.
效果
源码
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
来实现特定动画的功能,便于解耦 - 简单易懂
核心
时间: 2024-11-03 18:21:37