iOS进阶之旅-可交互式转场动画

本文介绍了如何在iOS中利用新的视图控制器转换API创建可交互式转场动画,特别是针对UINavigationController的push和pop操作。通过自定义动画控制器,实现了视图的渐入渐出效果,并详细解释了如何配置动画时间和执行动画的方法。同时,文章还展示了如何启用和处理交互式手势,以便用户可以平滑地控制转场过程。
摘要由CSDN通过智能技术生成

iOS7苹果为开发者引入了一系列新的API。其中一个尤为出色的API是一个新的视图控制器转换API。有趣的是,对于这个API苹果大量使用了协议,而不是具体的对象。虽然有点奇怪,但这种定义方式为我们提供了极大的灵活性,在深入探讨这个API之前,让我们看一下另一个iOS7的新特性。

注意导航控制器在iOS7中默认行为的改变,在导航控制器不同视图切换之间的动画在iOS7之后现在略有不懂,它是可交互的,即我们俗称的右滑返回。

所谓转场动画就是视图切换动画,交互式就是动画切换的进度依赖于用户的操作。本文将围绕UINavigationController的push、pop操作切换控制器进行讨论。但,苹果所提供的API是同样支持控制器的present、dismiss操作切换控制器与UITabbarViewController切换控制器的,其实现原理基本一致,在次就不多赘述了,希望和我一样在探索进阶之路的,如果看到这篇博客后能对你起到帮助,能自己去实现present、dismiss(实现UIViewControllerTransitioningDelegate与UITabbarViewController(实现UITabBarControllerDelegate的转场切换。

废话不多说,直入主题,我们今天做一个非常简单的demo,首先是一个不可交互的转场动画,push为渐入效果,pop为从屏幕大小缩小为CGZero。

push、pop操作么,肯定要与navigation controller扯上点关系了,我们说了新的API是代理,让我们来一起看一下UINavigationControllerDelegate中的一个方法

- (id< UIViewControllerAnimatedTransitioning>)navigationController:( UINavigationController*) navigationController
                                  animationControllerForOperation:( UINavigationControllerOperation) operation
                                               fromViewController:( UIViewController *) fromVC
                                                 toViewController:( UIViewController *) toVC
官方的描述: Implement this delegate method when you want to provide a custom animated transition between view controllers as they are added to or removed from the navigation stack. The object you return should be capable of configuring and performing noninteractive animations for the specified view controllers for the specified type of operation over a fixed period of time.
如果你想提供一个在视图被加入或从导航栈移除时提供自定义的动画转换视图的导航控制器,执行这个代理方法。该方法的返回对象能够帮助你配置视图动画和动画的持续时间。说的够明白了吧。
我们把目光聚集到返回值id<UIViewControllerAnimatedTransitioning>,一个实现UIViewControllerAnimatedTransitioning协议的代理,是的又是代理而不是对象。我们看一下两个声明为required的方法声明。

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext

官方描述:UIKit calls this method to obtain the timing information for your animations. The value you provide should be the same value that you use when configuring the animations in your animateTransition: method. UIKit uses the value to synchronize the actions of other objects that might be involved in the transition. For example, a navigation controller uses the value to synchronize changes to the navigation bar.

UIKit调用此方法来获得你的动画的时间信息。你配置的值应该与你在animatetransition:方法中设置的动画时间是一致的。UIKit使用该值来同步其他对象可能涉及过渡的行动。例如,导航控制器使用该值来对导航栏进行同步更改。简而言之,就是在这个方法里设置动画的时间


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

官方描述:UIKit calls this method when presenting or dismissing a view controller. Use this method to configure the animations associated with your custom transition. You can use view-based animations or Core Animation to configure your animations.

UIKit时调用此方法在将要显示或退出一个视图控制器时。使用此方法来配置与自定义转换相关联的动画。你可以使用基于视图的动画或CoreAnimation来配置你的动画。简而言之,就是把你想要的动画放到这里。

看到这里聪明的你脑袋里肯定已经有思路了,我们将一个类声明为UINavigationController的代理,在其中实现

- (id< UIViewControllerAnimatedTransitioning>)navigationController:( UINavigationController*) navigationController
                                  animationControllerForOperation:( UINavigationControllerOperation) operation
                                               fromViewController:( UIViewController *) fromVC
                                                 toViewController:( UIViewController *) toVC
然后创建一个实现 UIViewControllerContextTransitioning协议的对象,在该方法中返回。是的,你说的一点也不错。让我们来完成这个demo吧。

首先创建一个UICustomPushAnimation 实现 UIViewControllerContextTransitioning协议,在这个类中我们处理关于push的转场动画
.h文件

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>


@interface BWCustomPushAnimation : NSObject<UIViewControllerAnimatedTransitioning

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值