Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ModalAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
- func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
- fatalError("Abstract method")
- }
- let cornerRadius: CGFloat = 20
- let offset: CGFloat = 60
- func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
- return 0.4
- }
- }
- final class ModalPresentAnimationController: ModalAnimationController {
- override func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
- guard let fromVC = transitionContext.viewController(forKey: .from),
- let fromView = fromVC.view,
- let toVC = transitionContext.viewController(forKey: .to),
- let toView = toVC.view
- else {
- return
- }
- let offset = self.offset
- let cornerRadius = self.cornerRadius
- let duration = transitionDuration(using: transitionContext)
- let containerView = transitionContext.containerView
- let fromSnapshot = fromView.snapshotView(afterScreenUpdates: true)!
- containerView.addSubview(fromSnapshot)
- fromSnapshot.layer.masksToBounds = true
- fromView.removeFromSuperview()
- containerView.addSubview(toView)
- toView.layer.cornerRadius = cornerRadius
- toView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
- toView.layer.masksToBounds = true
- toView.frame = CGRect(x: 0,
- y: offset,
- width: containerView.frame.width,
- height: containerView.frame.height - offset)
- guard let toSnapshot = toView.snapshotView(afterScreenUpdates: true) else {
- return
- }
- containerView.addSubview(toSnapshot)
- toSnapshot.layer.cornerRadius = cornerRadius
- toSnapshot.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
- toSnapshot.layer.masksToBounds = true
- toSnapshot.frame = CGRect(x: 0,
- y: containerView.frame.height,
- width: containerView.frame.width,
- height: containerView.frame.height - offset)
- toView.isHidden = true
- UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseInOut], animations: {
- fromSnapshot.layer.cornerRadius = cornerRadius
- fromSnapshot.alpha = 0.5
- fromSnapshot.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
- toSnapshot.frame.origin.y = offset
- }) { _ in
- toView.isHidden = false
- toSnapshot.removeFromSuperview()
- transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
- }
- }
- }
- final class ModalDismissAnimationController: ModalAnimationController {
- override func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
- guard let fromVC = transitionContext.viewController(forKey: .from),
- let fromView = fromVC.view,
- let toVC = transitionContext.viewController(forKey: .to),
- let toView = toVC.view
- else {
- return
- }
- let offset = self.offset
- let duration = transitionDuration(using: transitionContext)
- let containerView = transitionContext.containerView
- guard let fromSnapshot = fromView.snapshotView(afterScreenUpdates: true),
- let toSnapshot = containerView.subviews.first else {
- return
- }
- containerView.addSubview(fromSnapshot)
- fromView.removeFromSuperview()
- fromSnapshot.frame = CGRect(x: 0,
- y: offset,
- width: containerView.frame.width,
- height: containerView.frame.height - offset)
- toView.isHidden = true
- UIView.animate(withDuration: duration, delay: 0, options: [.curveEaseInOut], animations: {
- fromSnapshot.frame.origin.y = containerView.frame.height
- toSnapshot.layer.cornerRadius = 0
- toSnapshot.alpha = 1
- toSnapshot.transform = CGAffineTransform.identity
- }) { _ in
- toView.isHidden = false
- transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
- }
- }
- }
- final class ModalViewController: UIViewController, UIViewControllerTransitioningDelegate {
- override var preferredStatusBarStyle: UIStatusBarStyle {
- return .lightContent
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- transitioningDelegate = self
- }
- @IBAction func tapCloseModal(sender: UIButton) {
- dismiss(animated: true, completion: nil)
- }
- }
- extension ModalViewController {
- func animationController(forPresented presented: UIViewController,
- presenting: UIViewController,
- source: UIViewController)
- -> UIViewControllerAnimatedTransitioning? {
- return ModalPresentAnimationController()
- }
- func animationController(forDismissed dismissed: UIViewController)
- -> UIViewControllerAnimatedTransitioning? {
- return ModalDismissAnimationController()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement