diff --git a/Sources/Thumbprint/Presentations/Sheet/PartialSheetPresentation.swift b/Sources/Thumbprint/Presentations/Sheet/PartialSheetPresentation.swift index f6b124f..32d8609 100644 --- a/Sources/Thumbprint/Presentations/Sheet/PartialSheetPresentation.swift +++ b/Sources/Thumbprint/Presentations/Sheet/PartialSheetPresentation.swift @@ -7,9 +7,11 @@ open class PartialSheetPresentation: NSObject, UIViewControllerTransitioningDele private let triggerPercentage: CGFloat = 0.42 let panGestureRecognizer: UIPanGestureRecognizer + let transitionCompletion: (() -> Void)? - required init(panGestureRecognizer: UIPanGestureRecognizer) { + required init(panGestureRecognizer: UIPanGestureRecognizer, transitionCompletion: (() -> Void)?) { self.panGestureRecognizer = panGestureRecognizer + self.transitionCompletion = transitionCompletion super.init() @@ -49,6 +51,11 @@ open class PartialSheetPresentation: NSObject, UIViewControllerTransitioningDele } } + override func finish() { + super.finish() + transitionCompletion?() + } + public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { transitionContext?.isInteractive == true ? 0.5 : 0.2 } @@ -76,7 +83,9 @@ open class PartialSheetPresentation: NSObject, UIViewControllerTransitioningDele return nil } - return PercentDrivenInteractiveTransition(panGestureRecognizer: partialSheetPresentationController.panGestureRecognizer) + return PercentDrivenInteractiveTransition(panGestureRecognizer: partialSheetPresentationController.panGestureRecognizer) { + partialSheetPresentationController.didCompleteInteractiveTransition() + } } public func interactionControllerForDismissal(using animator: UIViewControllerAnimatedTransitioning) -> UIViewControllerInteractiveTransitioning? { diff --git a/Sources/Thumbprint/Presentations/Sheet/PartialSheetPresentationController.swift b/Sources/Thumbprint/Presentations/Sheet/PartialSheetPresentationController.swift index e7f8a2e..587f623 100644 --- a/Sources/Thumbprint/Presentations/Sheet/PartialSheetPresentationController.swift +++ b/Sources/Thumbprint/Presentations/Sheet/PartialSheetPresentationController.swift @@ -12,8 +12,14 @@ public protocol PartialSheetPresentationControllerDelegate: UIAdaptivePresentati @objc optional func partialSheetPresentationControllerWillDismissSheet(_ partialSheetPresentationController: PartialSheetPresentationController) + /** + Called upon dismissal of the modal + - Parameters: + - partialSheetPresentationController: The partial sheet presentation controller responsible for the partial sheet presentation + - interactively: true if the modal was dismissed either by tapping on the background area or swiping down + */ @objc - optional func partialSheetPresentationControllerDidDismissSheet(_ partialSheetPresentationController: PartialSheetPresentationController) + optional func partialSheetPresentationControllerDidDismissSheet(_ partialSheetPresentationController: PartialSheetPresentationController, interactively: Bool) } open class PartialSheetPresentationController: UIPresentationController { @@ -93,6 +99,12 @@ open class PartialSheetPresentationController: UIPresentationController { let backgroundTapGestureRecognizer = UITapGestureRecognizer() let panGestureRecognizer = UIPanGestureRecognizer() + // Indicates that the modal was dismissed either by tapping the background area, or swiping down + var userDidDismissModal = false + func didCompleteInteractiveTransition() { + userDidDismissModal = true + } + public var partialSheetDelegate: PartialSheetPresentationControllerDelegate? { get { delegate as? PartialSheetPresentationControllerDelegate @@ -174,11 +186,13 @@ open class PartialSheetPresentationController: UIPresentationController { @objc private func backgroundTapGestureRecognizerHandler(sender: UITapGestureRecognizer) { guard let shouldDismissSheetMethod = partialSheetDelegate?.partialSheetPresentationControllerShouldDismissSheet else { + userDidDismissModal = true presentingViewController.dismiss(animated: true, completion: nil) return } if shouldDismissSheetMethod(self) { + userDidDismissModal = true presentingViewController.dismiss(animated: true, completion: nil) } } @@ -232,7 +246,7 @@ open class PartialSheetPresentationController: UIPresentationController { super.dismissalTransitionDidEnd(completed) if completed { - partialSheetDelegate?.partialSheetPresentationControllerDidDismissSheet?(self) + partialSheetDelegate?.partialSheetPresentationControllerDidDismissSheet?(self, interactively: userDidDismissModal) NotificationCenter.default.post(name: PartialSheetPresentationController.partialSheetDidDismissNotification, object: nil) } else {