自定义专场动画 UINavigationControllerDe

2017-09-07  本文已影响0人  林小姐的那片海

class CustomPushAnimation: NSObject, UIViewControllerAnimatedTransitioning {

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    transitionContext.containerView.backgroundColor = UIColor.white // 修改过渡时的背景颜色

    let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
    let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!

    let toVcName = NSStringFromClass(type(of: toViewController)).components(separatedBy: ".").last! as String

    /// 判断是否登陆页面
    if toVcName == "AuthMainController" {

// let snap = fromViewController.view.snapshotView(afterScreenUpdates: true)
//
// transitionContext.containerView.addSubview(snap!)

        toViewController.tabBarController?.tabBar.isHidden = true
        fromViewController.tabBarController?.tabBar.isHidden = true

        transitionContext.containerView.insertSubview(toViewController.view, aboveSubview: fromViewController.view)
        toViewController.view.transform = CGAffineTransform(translationX: 0, y: kScreenH)
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            toViewController.view.transform = CGAffineTransform(translationX: 0, y: kTopH)
            fromViewController.view.transform = CGAffineTransform(translationX: 0, y: -kScreenH)
        }) { (completion) in
            fromViewController.view.transform = CGAffineTransform.identity
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
        }
    } else {
        /// 全透明
        transitionContext.containerView.insertSubview(toViewController.view, aboveSubview: fromViewController.view)
        if fromViewController.navBarBackgroundAlpha == 0 && toViewController.navBarBackgroundAlpha == 0 {
            toViewController.view.transform = CGAffineTransform(translationX: kScreenW, y: kTopH)
            UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
                toViewController.view.transform = CGAffineTransform.identity
                fromViewController.view.transform = CGAffineTransform(translationX: -kScreenW, y: 0)
            }) { (completion) in
                fromViewController.view.transform = CGAffineTransform.identity
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            }
        } else {
            /// from透明
            if fromViewController.navBarBackgroundAlpha == 0 {
                toViewController.view.transform = CGAffineTransform(translationX: kScreenW, y: 0)
                UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
                    toViewController.view.transform = CGAffineTransform(translationX: 0, y: kTopH)
                    fromViewController.view.transform = CGAffineTransform.identity
                }) { (completion) in
                    fromViewController.view.transform = CGAffineTransform.identity
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                }
            } else {

                toViewController.view.transform = CGAffineTransform(translationX: kScreenW, y: kTopH)
                UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
                    toViewController.view.transform = CGAffineTransform(translationX: 0, y: kTopH)
                    fromViewController.view.transform = CGAffineTransform.identity
                }) { (completion) in
                    fromViewController.view.transform = CGAffineTransform.identity
                    transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                }
            }

        }
    }
}

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 0.5
}

}

上一篇下一篇

猜你喜欢

热点阅读