iOS转场动画—Present和Dismiss转场

字数 90阅读 450

Present和Dismiss转场

效果图.gif

1、新建两个controller,并在第一个controller中实现present方法跳转到第二个controller。
2、在第二个controller中重写初始化方法

    init() {
        super.init(nibName:nil, bundle:nil)
        self.transitioningDelegate = self
        self.modalPresentationStyle = .custom
    }

3、遵守UIViewControllerTransitioningDelegate协议,实现协议方法.

func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CircleSpreadTransition(.present)
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return CircleSpreadTransition(.dismiss)
    }

4、新建一个过渡动画的类(以CircleSpreadTransition为例),在改类中实现动画效果。

import UIKit
enum CircleSpreadTransitionType:Int {
    case present = 0
    case dismiss
}
class CircleSpreadTransition: NSObject, UIViewControllerAnimatedTransitioning, CAAnimationDelegate {
    var type:CircleSpreadTransitionType?
    init(_ myType:CircleSpreadTransitionType) {
        super.init()
        type = myType
    }

    
    //MARK:-UIViewControllerAnimatedTransitioning
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {

        return 0.5

    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        switch type! {
        case .present:
            self.presentAnimation(transitionContext)
            break
        case .dismiss:
            self.dismissAnimation(transitionContext)
            break
        }
    }
    
    /**
     *  实现present动画
     */
    func presentAnimation(_ transitionContext:UIViewControllerContextTransitioning){
        //通过viewControllerForKey取出转场前后的两个控制器,这里toVC就是vc1、fromVC就是vc2
        //这里返回的是UIViewController,不关心具体是哪个类,不用强转也可以
        let toVC = transitionContext.viewController(forKey: .to)!
        let fromVC:UINavigationController = transitionContext.viewController(forKey: .from) as! UINavigationController
        
        let temp:CircleSpreadController = fromVC.viewControllers.last as! CircleSpreadController
        let containerView = transitionContext.containerView
        containerView.addSubview(toVC.view)
        //画两个圆路径
        let startCycle:UIBezierPath = UIBezierPath(ovalIn: temp.buttonFrame())
        let x = MAX(temp.buttonFrame().origin.x, num2: containerView.frame.size.width-temp.buttonFrame().origin.x)
        let y = MAX(temp.buttonFrame().origin.y, num2: containerView.frame.size.height-temp.buttonFrame().origin.y)
        let radius:CGFloat = CGFloat(sqrtf(Float(pow(x, 2))+Float(pow(y,2))))
        let endCycle:UIBezierPath = UIBezierPath(arcCenter: containerView.center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
        //创建CAShapeLayer进行遮盖
        let maskLayer = CAShapeLayer()
        maskLayer.path = endCycle.cgPath
        //将maskLayer作为toVC.View的遮盖
        toVC.view.layer.mask = maskLayer
        //创建路径动画
        let maskLayerAnimation = CABasicAnimation(keyPath: "path")
        maskLayerAnimation.delegate = self
        //动画是加到layer上的,所以必须为CGPath
        maskLayerAnimation.fromValue = startCycle.cgPath
        maskLayerAnimation.toValue = endCycle.cgPath
        maskLayerAnimation.duration = self.transitionDuration(using: transitionContext)
        maskLayerAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
        maskLayerAnimation.setValue(transitionContext, forKey: "transitionContext")
        maskLayer.add(maskLayerAnimation, forKey: "path")
        
    
    }
    
    func MAX(_ num1:CGFloat, num2:CGFloat) -> CGFloat{
        if num1 > num2 {
            return num1
        }else{
            return num2
        }
    }
    
    
    /**
     *  实现dimiss动画
     */
    
    func dismissAnimation(_ transitionContext:UIViewControllerContextTransitioning){
        //注意在dismiss的时候fromVC就是vc2了,toVC才是VC1了,注意理解这个逻辑关系
        let toVC = transitionContext.viewController(forKey: .to) as! UINavigationController
        let fromVC = transitionContext.viewController(forKey: .from)!
        
        let temp:CircleSpreadController = toVC.viewControllers.last as! CircleSpreadController
        let containerView = transitionContext.containerView
        //画两个圆路径
        let radius:CGFloat = CGFloat(sqrtf(Float(containerView.frame.size.height * containerView.frame.size.height + containerView.frame.size.width * containerView.frame.size.width)) / 2)
        
        
        let startCycle:UIBezierPath = UIBezierPath(arcCenter: containerView.center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
        let endCycel:UIBezierPath = UIBezierPath(ovalIn: temp.buttonFrame())
        //创建CAShapeLayer进行遮盖
        let maskLayer = CAShapeLayer()
        maskLayer.fillColor = UIColor.green.cgColor
        maskLayer.path = endCycel.cgPath
        fromVC.view.layer.mask = maskLayer
        //创建动画路径
        let maskLayerAnimation = CABasicAnimation(keyPath: "path")
        maskLayerAnimation.delegate = self
        maskLayerAnimation.fromValue = startCycle.cgPath
        maskLayerAnimation.toValue = endCycel.cgPath
        maskLayerAnimation.duration = self.transitionDuration(using: transitionContext)
        maskLayerAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
        maskLayerAnimation.setValue(transitionContext, forKey: "transitionContext")
        maskLayer.add(maskLayerAnimation, forKey: "path")

        
    }
    
    //MARK:-动画结束的代理方法
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        switch type! {
        case .present:
            let transitionContext:UIViewControllerContextTransitioning = anim.value(forKey: "transitionContext") as! UIViewControllerContextTransitioning
            transitionContext.completeTransition(true)
            break
        case .dismiss:
            let transitionContext:UIViewControllerContextTransitioning = anim.value(forKey: "transitionContext") as! UIViewControllerContextTransitioning
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            if transitionContext.transitionWasCancelled {
                transitionContext.viewController(forKey: .from)?.view.layer.mask = nil
            }
            break
        }
    }

推荐阅读更多精彩内容