You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.1 KiB

//
// CustomMaskTransiton.swift
// AnimationTransitionDemo
//
// Created by junjie on 2019/9/10.
// Copyright © 2019 junjie. All rights reserved.
//
import UIKit
class CustomMaskTransiton: NSObject, UIViewControllerAnimatedTransitioning {
var transition: UIViewControllerContextTransitioning?
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.5
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
transition = transitionContext
let fromVC = transitionContext.viewController(forKey: .from) as! CustomMaskViewController
let toVC = transitionContext.viewController(forKey: .to) as! NextViewController
let containerView = transitionContext.containerView
containerView.addSubview(toVC.view)
//
let buttonFrame = fromVC.currentView.frame
let startPath = UIBezierPath(roundedRect: buttonFrame, cornerRadius: 40)
let endPath = UIBezierPath(roundedRect: CGRect(origin: CGPoint(x:-230, y: 0), size: CGSize(width: UIScreen.main.bounds.height, height: UIScreen.main.bounds.height+100)), cornerRadius: UIScreen.main.bounds.height/2)
let shapeLayer = CAShapeLayer()
toVC.view.layer.mask = shapeLayer
//
shapeLayer.path = endPath.cgPath
let basicAnimation = CABasicAnimation(keyPath: "path")
basicAnimation.fromValue = startPath.cgPath
basicAnimation.toValue = endPath.cgPath
basicAnimation.duration = self.transitionDuration(using: transitionContext)
basicAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
basicAnimation.delegate = self
shapeLayer.add(basicAnimation, forKey: nil)
}
}
extension CustomMaskTransiton: CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
let toVC = transition?.viewController(forKey: .to) as! NextViewController
toVC.view.layer.mask = nil
transition?.completeTransition(!transition!.transitionWasCancelled)
}
}