Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Add "interactively" parameter to partial sheet presentation delegate (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dwroth authored Jul 30, 2024
1 parent 265a9a0 commit 5689997
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5689997

Please sign in to comment.