Advertisement
aleksgeyman

UIViewController+CustomAnimations

Mar 12th, 2020
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.48 KB | None | 0 0
  1. //
  2. //  UIViewController+CustomAnimations.swift
  3. //  NavBarCustomOrientationDemo
  4. //
  5. //  Created by Aleksandar Geyman on 11.03.20.
  6. //  Copyright © 2020 Aleksandar Geyman. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. private struct AnimationConstants {
  12.     static let duration = 0.25
  13. }
  14.  
  15. private enum AnimationType {
  16.     case present
  17.     case dismiss
  18. }
  19.  
  20. extension UIViewController {
  21.    
  22.     func presentWithCustomAnimation(viewControllerToPresent: UIViewController, completion: (() -> Void)?) {
  23.         view.window?.layer.add(configureAnimation(for: .present), forKey: kCATransition)
  24.         present(viewControllerToPresent, animated: false, completion: completion)
  25.     }
  26.    
  27.     func dismissWithCustomAnimation(completion: (() -> Void)?) {
  28.         view.window?.layer.add(configureAnimation(for: .dismiss), forKey: kCATransition)
  29.         dismiss(animated: false, completion: completion)
  30.     }
  31.    
  32.     private func configureAnimation(for animationType: AnimationType) -> CATransition {
  33.         var type: CATransitionType
  34.         var subType: CATransitionSubtype
  35.         switch animationType {
  36.         case .present:
  37.             type = .push
  38.             subType = .fromRight
  39.         case .dismiss:
  40.             type = .push
  41.             subType = .fromLeft
  42.         }
  43.        
  44.         let transition = CATransition()
  45.         transition.duration = AnimationConstants.duration
  46.         transition.type = type
  47.         transition.subtype = subType
  48.         return transition
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement