-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from erikdrobne/feature/modal-transitions
Add modal transition delegate associated value to the present action.
- Loading branch information
Showing
6 changed files
with
107 additions
and
4 deletions.
There are no files selected for viewing
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
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
52 changes: 52 additions & 0 deletions
52
...ple/SwiftUICoordinatorExample/SwiftUICoordinatorExample/Transitions/SlideTransition.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// SlideTransition.swift | ||
// SwiftUICoordinatorExample | ||
// | ||
// Created by Erik Drobne on 9. 11. 23. | ||
// | ||
|
||
import UIKit | ||
|
||
class SlideTransition: NSObject, UIViewControllerAnimatedTransitioning { | ||
let isPresenting: Bool | ||
|
||
init(isPresenting: Bool) { | ||
self.isPresenting = isPresenting | ||
super.init() | ||
} | ||
|
||
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { | ||
0.5 | ||
} | ||
|
||
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { | ||
let key = isPresenting ? UITransitionContextViewControllerKey.to : UITransitionContextViewControllerKey.from | ||
guard let controller = transitionContext.viewController(forKey: key) else { return } | ||
|
||
if isPresenting { | ||
transitionContext.containerView.addSubview(controller.view) | ||
} | ||
|
||
let finalFrame = transitionContext.finalFrame(for: controller) | ||
let startingFrame = isPresenting ? finalFrame.offsetBy(dx: 0, dy: -finalFrame.height) : finalFrame | ||
let endingFrame = isPresenting ? finalFrame : finalFrame.offsetBy(dx: 0, dy: -finalFrame.height) | ||
|
||
controller.view.frame = startingFrame | ||
|
||
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { | ||
controller.view.frame = endingFrame | ||
}) { _ in | ||
transitionContext.completeTransition(!transitionContext.transitionWasCancelled) | ||
} | ||
} | ||
} | ||
|
||
final class SlideTransitionDelegate: NSObject, UIViewControllerTransitioningDelegate { | ||
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? { | ||
return SlideTransition(isPresenting: true) | ||
} | ||
|
||
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? { | ||
return SlideTransition(isPresenting: false) | ||
} | ||
} |
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
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
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