iOS开发进阶Unity跨平台技术分享iOS Developer

iOS - Swift 控制器转场动画

2017-12-29  本文已影响65人  GA_
返回效果也可更改

四种转场动画

1. move:源图片位置移动到目标图片位置;
2. circle:根据源控件大小创建圆形或者椭圆形path路径,放大展示目标;
3. tier:源左右,目标由小到大缩放;
4. middle:源的中心点开始放大,返回是缩回到中心。

代码解析

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
        }
    }
}
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
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中可以看到。
--欢迎下载--
--感谢大神指点--

上一篇 下一篇

猜你喜欢

热点阅读