虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > IOS编程/Objective-C > iOS Swift控制器转场动画示例代码

iOS Swift控制器转场动画示例代码
类别:IOS编程/Objective-C   作者:码皇   来源:互联网   点击:

这篇文章主要给大家介绍了关于iOS Swift控制器转场动画的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

在IOS开发中,我们model另外一个控制器的时候,一般都使用默认的转场动画。本文将给大家详细介绍关于iOS Swift控制器转场动画的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

返回效果也可更改

四种转场动画

      1. move:源图片位置移动到目标图片位置;

      2. circle:根据源控件大小创建圆形或者椭圆形path路径,放大展示目标;

      3. tier:源左右,目标由小到大缩放;

      4. middle:源的中心点开始放大,返回是缩回到中心。

代码解析

给UIViewController添加一个属性yy_routerAnimation: YYTransition

    extension UIViewController {
    public var yy_routerAnimation : YYTransition {
    set {
    objc_setAssociatedObject(self, &YYTransitionKey.kRouterAnimationKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) }
    get {
    guard let r = objc_getAssociatedObject(self, &YYTransitionKey.kRouterAnimationKey) as? YYTransition else {
    return YYTransition() }
    return r }
    }
    }

YYTransition类

    public class YYTransition: NSObject

遵守代理

    extension YYTransition: UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate, UINavigationControllerDelegate

实现代理方法

    return self }
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 转场动画所需时间 }

这个方法内调用相应动画方法

    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    switch self.yy_ransitionAnimationType {
    case .circle: break case .move: break case .middle: break case .tier: break }
    }

相关属性

    extension YYTransition {
    // 是push还是pop public var yy_isBack: Bool {
    }
    // 动画类型 var yy_ransitionAnimationType: YYTransitionAnimationType {
    }
    // 源view名字 var yy_fromViewPath: String? {
    }
    // 目标view名字 var yy_toViewPath: String? {
    }
    // 句柄 var yy_transitionContext: UIViewControllerContextTransitioning {
    }
    }

实现基础动画结束时的代理方法

    extension YYTransition: CAAnimationDelegate {
    public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    yy_transitionContext.completeTransition(!yy_transitionContext.transitionWasCancelled) yy_transitionContext.viewController(forKey: .from)?.view.layer.mask = nil yy_transitionContext.viewController(forKey: .to)?.view.layer.mask = nil }
    }

基础动画对路径操作的动画

    extension YYTransition {
    func maskAnimation(targetVC: UIViewController, startPath: UIBezierPath, endPath: UIBezierPath, context: UIViewControllerContextTransitioning) {
    }

下面四个文件内实现相对应的动画

    YYTransition+CircleYYTransition+MoveYYTransition+TierYYTransition+Middle

动画实现的思想基本就是拿到源view和目标view,控制位置和大小,做相应的动画即可。
用到的方法

    UIViewControllerContextTransitioning 调用public func viewController(forKey key: UITransitionContextViewControllerKey) -> UIViewController?UIViewController调用open func value(forKeyPath keyPath: String) -> Any?* When requesting a snapshot, 'afterUpdates' defines whether the snapshot is representative of what's currently on screen or if you wish to include any recent changes before taking the snapshot. open func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView?open func convert(_ rect: CGRect, from view: UIView?) -> CGRectopen func insertSubview(_ view: UIView, belowSubview siblingSubview: UIView)// This must be called whenever a transition completes (or is cancelled.) // Typically this is called by the object conforming to the // UIViewControllerAnimatedTransitioning protocol that was vended by the transitioning // delegate. For purely interactive transitions it should be called by the // interaction controller. This method effectively updates internal view // controller state at the end of the transition.public func completeTransition(_ didComplete: Bool)

具体代码在YE项目地址中YYTransition动态库中

eg在YYSourceTransitionViewController和YYTargetTransitionViewController中可以看到。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

您可能感兴趣的文章:

  • IOS动画效果源代码整理(粒子、雪花、火焰、河流、蒸汽)
  • iOS 控制器自定义动画跳转方法(模态跳转)
  • iOS如何自定义控制器转场动画push详解
  • iOS动画解析之圆球加载动画XLBallLoading的实现
  • iOS如何为导航栏添加播放动画
  • IOS 中动画的暂停与继续播放的详解
  • IOS登录页面动画、转场动画开发详解
相关热词搜索: ios 控制器转场动画 swift 转场动画 ios 转