首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VCTransitionsLibrary –自定义iOS交互式转场动画的库

VCTransitionsLibrary –自定义iOS交互式转场动画的库

作者头像
ios122
发布2018-01-02 17:48:57
1.5K0
发布2018-01-02 17:48:57
举报

简介

VCTransitionsLibrary 提供了许多适用于入栈,出栈,模态等场景下控制器切换时的转场动画.它本身提供了一个定义好的转场动画库,你可以拖到自己工程中直接使用;也提供了许多拥有不同转场动画效果”互动控制器”,你可以直接使用这些控制器来和自定义动画效果配合使用;而不是自己控制去控制交互.

最新示例: 点击下载

注意: 自定义视图控制器的转场动画为iOS7 + 通过 UIViewControllerTransitioningDelegate协议, UINavigationControllerDelegate协议和 UITabBarControllerDelegate 协议提供的系统级别的支持.这个库的意义在于定义了常用的动画效果,并封装了常用的交互操作,简化了iOS交互式转场动画的编码量!

快速入门

运行环境

  • iOS 7+
  • ARC

安装

使用 CocoaPods 安装

pod "VCTransitionsLibrary"

手动安装

把文件 AnimationControllers 和 InteractionControllers 文件夹下所有代码复制到工程中即可.

使用

在自定义转场动画时,有两类关键的类:

  • 动画控制器 –  这个类是用来实现自定义动画的.但你声明想要使用自定义动画时,你应该提供一个动画控制器.这个类会实现需要的动画,完成时会通知框架.
  • 交互控制器 – 这个类是用来管理交互的-那些通常由某个手势空控制的交互,允许用户通过滑动,轻扫或执行其他操作来实现两个视图控制器的导航.必须指出的是,交互控制器允许导航取消,例如,一个用户可以在正在导航至某一页面时,突然改变主意,然后取消了操作.

注意: 动画和交互是完全独立的,这意味着你可以在其他任何自定义控制器上独立使用交互控制器-很酷!

使用动画控制器

AnimationControllers 文件夹中提供了许多可以整合进你的工程中的动画控制器:

自定义模态控制器显示/隐藏的动画

UIViewControllerTransitioningDelegate 协议被用来在模态控制器显示/隐藏时提供一个动画控制器.当一个视图控制器被模态显示或隐藏时,它的transitioningDelegate属性用来提供UIViewControllerTransitioningDelegate协议的支持.担当代理角色的类,通过 animationControllerForPresentedController: presentingController: sourceController: 方法返回模态显示时的动画, 通过 animationControllerForDismissedController: 返回模态消失时的动画即可.

自定义顶部导航的转场动画

UINavigationController 有一个

id<UINavigationControllerDelegate> delegate 属性.只需要让它的代理通过 navigationController: animationControllerForOperation: fromViewController: toViewController: 返回某个动画效果即可.

为了同时设置出栈/入栈都合适的动画效果(或者说,出栈/入栈时能使用相反方向的动画),你可以参考下面代码:

- (id<UIViewControllerAnimatedTransitioning>)navigationController:
                                (UINavigationController *)navigationController
   animationControllerForOperation:(UINavigationControllerOperation)operation
                fromViewController:(UIViewController *)fromVC
                  toViewController:(UIViewController *)toVC {

    // 出栈时,要反转动画方向.
    _animationController.reverse = operation == UINavigationControllerOperationPop;

    return _animationController;
}
自定义底部标签栏导航的转场动画

UITabBarController 有一个 id<UITabBarControllerDelegate> delegate属性,只需要让它的代理通过tabBarController: animationControllerForTransitionFromViewController: toViewController:返回某个动画效果即可.

为了给动画一个合适的方向,你可以比较两个视图控制器的索引:

- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
            animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                              toViewController:(UIViewController *)toVC {

    NSUInteger fromVCIndex = [tabBarController.viewControllers indexOfObject:fromVC];
    NSUInteger toVCIndex = [tabBarController.viewControllers indexOfObject:toVC];

    _animationController.reverse = fromVCIndex < toVCIndex;
    return _animationController;
}

使用交互控制器

交互控制器和动画控制器配合使用,可以实现交互式的动画转场效果,比如可以让用户通过手势来控制页面间的导航.交互控制器允许用户在一个转场动画中前进,后退,甚至退出.

交互控制器负责给视图添加手势,并负责在用户使用某个手势时进行相应地导航操作.

模态控制器消失时的交互

UIViewControllerTransitioningDelegate 协议,也用来提供对交互式转场的支持.下面是一个结合清扫手势和翻页动画的例子:

//实例变量,通常在你的初始化方法初始化它们
CEFlipAnimationController *_animationController;
CESwipeInteractionController *_interactionController;

- (id<UIViewControllerAnimatedTransitioning>)
      animationControllerForPresentedController:(UIViewController *)presented
                           presentingController:(UIViewController *)presenting
                               sourceController:(UIViewController *)source {

    // 允许交互控制器绑定它的手势识别器.
    [_interactionController wireToViewController:presented 
                                    forOperation:CEInteractionOperationDismiss];
       _animationController.reverse = NO;
    return _animationController;
}

- (id<UIViewControllerAnimatedTransitioning>)
     animationControllerForDismissedController:(UIViewController *)dismissed {
    _animationController.reverse = YES;
    return _animationController;
}

- (id<UIViewControllerInteractiveTransitioning>)
           interactionControllerForDismissal:
                (id<UIViewControllerAnimatedTransitioning>)animator {

    // 如果有交互控制器被触发了,就直接使用它.返回nil,是为了支持用户通过点击某个按钮直接返回;此时不会触发交互控制器.
    return _interactionController.interactionInProgress
                ? _interactionController : nil;
}
出栈时的交互

UINavigationControllerDelegate 也有方法为交互式转场提供支持.一个典型的类似于上上面代码的模式:

// 实例变量,通常在你的初始化方法中初始化它们.
CEFlipAnimationController *_animationController;
CESwipeInteractionController *_interactionController;

- (id<UIViewControllerAnimatedTransitioning>)
                 navigationController:(UINavigationController *)navigationController
      animationControllerForOperation:(UINavigationControllerOperation)operation
                   fromViewController:(UIViewController *)fromVC
                     toViewController:(UIViewController *)toVC {

    // 把交互控制器绑定到你的视图控制器上.
    [_interactionController wireToViewController:toVC
                                    forOperation:CEInteractionOperationPop];

    _animationController.reverse = operation == UINavigationControllerOperationPop;

    return _animationController;
}

- (id <UIViewControllerInteractiveTransitioning>)
                         navigationController:(UINavigationController *)navigationController 
  interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>)animationController {

    //如果有交互控制器被触发了,就直接使用它.返回nil,是为了支持用户通过点击某个按钮直接返回;此时不会触发交互控制器. 
    return _interactionController.interactionInProgress
                ? _interactionController : nil;
}
用于标签栏控制器切换时的交互

UITabBarControllerDelegate 协议也为交互式转场提供了支持.但是由于代理方法在首次初始化时不被执行,所有需要其他方式来绑定交互控制器,如KVO:

@implementation TabBarViewController {
    CEFoldAnimationController *_animationController;
    CESwipeInteractionController *_swipeInteractionController;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.delegate = self;

        // 创建交互/动画控制器.
        _swipeInteractionController = [CESwipeInteractionController new];
        _animationController = [CEFoldAnimationController new];
        _animationController.folds = 3;

        // 使用观察者模式监测被选中的选择器的变化情况.
        [self addObserver:self
               forKeyPath:@"selectedViewController"
                  options:NSKeyValueObservingOptionNew
                  context:nil];
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqualToString:@"selectedViewController"] )
    {
        // 把交互控制器绑定到视图控制器上.
        [_swipeInteractionController wireToViewController:self.selectedViewController
                                             forOperation:CEInteractionOperationTab];
    }
}



- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
            animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                              toViewController:(UIViewController *)toVC {

    NSUInteger fromVCIndex = [tabBarController.viewControllers indexOfObject:fromVC];
    NSUInteger toVCIndex = [tabBarController.viewControllers indexOfObject:toVC];

    _animationController.reverse = fromVCIndex < toVCIndex;
    return _animationController;
}

-(id<UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
    return _swipeInteractionController.interactionInProgress ? _swipeInteractionController : nil;
}

@end
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-08-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 简介
  • 快速入门
    • 运行环境
      • 安装
        • 使用 CocoaPods 安装
      • 手动安装
        • 使用
          • 使用动画控制器
          • 使用交互控制器
      相关产品与服务
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档