2018正版葡京赌侠诗iOS – 斯维夫特(Swift) 控制器转场动画

by admin on 2019年1月14日

回去效果也可更改

四种转场动画

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+Circle
YYTransition+Move
YYTransition+Tier
YYTransition+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?) -> CGRect

open 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中可以见见。
–欢迎下载–
–感谢大神带领–

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图