Last active
February 26, 2023 15:50
-
-
Save atomkirk/a7489a30fb26b839b78cdac1df4e0561 to your computer and use it in GitHub Desktop.
Custom Fade In/Out Segue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// All you do is assign this class to a segue in your storyboard and you're done | |
class BottomCardSegue: UIStoryboardSegue { | |
private var selfRetainer: BottomCardSegue? = nil | |
override func perform() { | |
destination.transitioningDelegate = self | |
selfRetainer = self | |
destination.modalPresentationStyle = .overCurrentContext | |
source.present(destination, animated: true, completion: nil) | |
} | |
} | |
extension BottomCardSegue: UIViewControllerTransitioningDelegate { | |
public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
return TransitioningAnimatorIn() | |
} | |
public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { | |
selfRetainer = nil | |
return TransitioningAnimatorOut() | |
} | |
} | |
class TransitioningAnimatorIn: NSObject, UIViewControllerAnimatedTransitioning { | |
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { | |
return 0.5 | |
} | |
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { | |
guard | |
let toViewController = transitionContext.viewController(forKey: .to) | |
else { | |
return | |
} | |
transitionContext.containerView.addSubview(toViewController.view) | |
toViewController.view.alpha = 0 | |
let duration = self.transitionDuration(using: transitionContext) | |
UIView.animate(withDuration: duration, animations: { | |
toViewController.view.alpha = 1 | |
}, completion: { _ in | |
transitionContext.completeTransition(!transitionContext.transitionWasCancelled) | |
}) | |
} | |
} | |
class TransitioningAnimatorOut: NSObject, UIViewControllerAnimatedTransitioning { | |
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { | |
return 0.5 | |
} | |
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { | |
guard | |
let fromViewController = transitionContext.viewController(forKey: .from) | |
else { | |
return | |
} | |
let duration = self.transitionDuration(using: transitionContext) | |
UIView.animate(withDuration: duration, animations: { | |
fromViewController.view.alpha = 0 | |
}, completion: { _ in | |
transitionContext.completeTransition(!transitionContext.transitionWasCancelled) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment