123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- //
- // SlideInTransition.swift
- // app
- //
- // Created by Luis Quiñones on 10/25/19.
- // Copyright © 2019 Luis Quiñones . All rights reserved.
- //
-
- import UIKit
-
- class SlideInTransition: NSObject, UIViewControllerAnimatedTransitioning {
-
- var isPresenting = false
- let dimmingView = UIView()
-
- func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
- return 0.3
- }
-
- func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
-
- guard let toViewController = transitionContext.viewController(forKey: .to),
- let fromViewController = transitionContext.viewController(forKey: .from) else { return }
-
- let containerView = transitionContext.containerView
-
- let finalWidth = toViewController.view.bounds.width * 0.8
- let finalHeight = toViewController.view.bounds.height
-
- if isPresenting {
-
- // Add dimming view
- dimmingView.backgroundColor = .black
- dimmingView.alpha = 0.0
- containerView.addSubview(dimmingView)
- dimmingView.frame = containerView.bounds
-
- // Add menu view controller to container
- containerView.addSubview(toViewController.view)
-
- // Init fram off the screen
- toViewController.view.frame = CGRect(x: -finalWidth, y: 0, width: finalWidth, height: finalHeight)
- }
-
- // Animate on screen
- let transform = {
- self.dimmingView.alpha = 0.5
- toViewController.view.transform = CGAffineTransform(translationX: finalWidth, y: 0)
- }
-
- // Animate off screen
- let identity = {
- self.dimmingView.alpha = 0.0
- fromViewController.view.transform = .identity
- }
-
- let duration = transitionDuration(using: transitionContext)
- let isCancelled = transitionContext.transitionWasCancelled
- UIView.animate(withDuration: duration, animations: {
- self.isPresenting ? transform() : identity()
- }) { (_) in
- transitionContext.completeTransition(!isCancelled)
- }
-
- }
- }
|