UIViewControllerAnimatedTransitioning UIViewControllerTransitionCoordinator 回顾

本文探讨了在iOS项目中使用transitioning和coordinator进行自定义动画设计的经验,详细介绍了这两种动画机制的区别及如何在不同场景下灵活运用。通过实际案例展示了如何创建丰富的动画效果,并提供了一个测试demo链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   工作原因,切换到了新的项目,终于有机会用一发系统设计的动画框架了

具体代码就不说了,这里说说使用这2个东西后大体的认识吧

transitioning 和 coordinator 都是用于自定义动画,但是二者定义的维度不一样,A主要用于完全自定义系统的viewController动画显示,例如present,push等等了,当然你说是不是用了transitioning就不能用coordingator呢,当然也不是,coordinator本质的系统设计想法是在大的viewcontroller转场动画后,用于viewController内部小的UI元素做动画,这些个小的UI元素的动画和大的转场动画在同一个动画上下文中,这样是不是就可以让我们的动画变得很丰富呢?我不说你也知道,��(装波逼 ~~)
全动画逻辑在苹果哪里的说法叫 non interaction animation;大家肯定直接可以想到non interaction animation反面就是 interaction animation,我这里把这个翻译成交互式动画,一个典型的例子就是我们导航控件手势向右滑动退出了,实现这个交互式动画需要配合系统提供的UIPercentDrivenInteractiveTransition,手势外加UIPercentDrivenInteractiveTransition就可以实现你意向不到的交互式动画了

系统的导航控件 navigationcontroller也提供了自定义的delegate ,本质和viewcontroller一样

站在更高的层面思考,这个自定义动画的逻辑就是系统将相关动画的控制参数通过delegate全部交给你来做,我们自己做一个应该也是 so easy 的

我的测试demo:https://github.com/CaichaoGitHub/IOSDemos/tree/master/testCustomViewControllerAnimation

### 使用 Transition Controller 创建 iOS 过渡动画 #### 定义 Animation Controller 类 为了创建自定义过渡动画,需先定义一个 `AnimationController` 类。此类继承自 `NSObject` 并遵循 `UIViewControllerAnimatedTransitioning` 协议,实现两个必需的方法:指定动画持续时间和执行实际的动画逻辑。 ```swift class AnimationController: NSObject, UIViewControllerAnimatedTransitioning { func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { // 设置动画时长 return 0.5 } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { guard let fromView = transitionContext.view(forKey: .from), let toView = transitionContext.view(forKey: .to) else { return } let containerView = transitionContext.containerView containerView.addSubview(toView) toView.alpha = 0 UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { fromView.alpha = 0 toView.alpha = 1 }) { finished in transitionContext.completeTransition(!transitionContext.transitionWasCancelled) } } } ``` #### 实现 Navigation Delegate 方法 为了让导航控制器知道何时应用此自定义场效果,在项目中还需引入另一个类作为 `UINavigationControllerDelegate` 的实例。这个委托负责返回具体的 `UIViewControllerAnimatedTransitioning` 对象给导航栈中的视图控制器切换操作。 ```swift class NavigationControllerDelegate: NSObject, UINavigationControllerDelegate { private var interactiveTransition: UIPercentDrivenInteractiveTransition? func navigationController( _ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController ) -> UIViewControllerAnimatedTransitioning? { switch operation { case .push: return AnimationController() default: return nil } } // Optional method for handling interactive transitions. func navigationController( _ navigationController: UINavigationController, interactionControllerFor animationController: UIViewControllerAnimatedTransitioning ) -> UIViewControllerInteractiveTransitioning? { return interactiveTransition } } ``` #### 应用到具体场景 最后一步是在目标视图控制器里设置好上述提到的代理关系,通常可以在准备推送新页面之前完成这一步骤: ```swift let destinationViewController = YourDestinationViewController() navigationController?.delegate = NavigationControllerDelegate() navigationController?.pushViewController(destinationViewController, animated: true) ``` 通过这种方式可以轻松地为应用程序内的任何视图控制器之间的换添加独特的视觉体验[^1][^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值