Skip to content

Commit

Permalink
Merge pull request #1 from ca-love/fix-gesture-cancelled
Browse files Browse the repository at this point in the history
Fix bug of freeze because of tracking gesture is cancelled
  • Loading branch information
s2mr authored Nov 11, 2021
2 parents b2f5bd7 + 7317d3b commit a164ce8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
10 changes: 6 additions & 4 deletions PanModal/Animator/PanModalPresentationAnimator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ public class PanModalPresentationAnimator: NSObject {

PanModalAnimator.animate({
panView.frame.origin.y = yPos
}, config: presentable) { [weak self] didComplete in
}, config: presentable) { [weak self] _ in
// Calls viewDidAppear and viewDidDisappear
fromVC.endAppearanceTransition()
transitionContext.completeTransition(didComplete)
// Regardless of animation canceling, complete transition
transitionContext.completeTransition(true)
self?.feedbackGenerator = nil
}
}
Expand All @@ -117,11 +118,12 @@ public class PanModalPresentationAnimator: NSObject {

PanModalAnimator.animate({
panView.frame.origin.y = transitionContext.containerView.frame.height
}, config: presentable) { didComplete in
}, config: presentable) { _ in
fromVC.view.removeFromSuperview()
// Calls viewDidAppear and viewDidDisappear
toVC.endAppearanceTransition()
transitionContext.completeTransition(didComplete)
// Regardless of animation canceling, complete transition
transitionContext.completeTransition(true)
}
}

Expand Down
46 changes: 30 additions & 16 deletions PanModal/Controller/PanModalPresentationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,28 @@ private extension PanModalPresentationController {
return
}

let transitionForPosition = { [weak self] in
guard let me = self else { return }
let shortFormYPosition = me.shortFormYPosition
let longFormYPosition = me.longFormYPosition

/**
The `containerView.bounds.height` is used to determine
how close the presented view is to the bottom of the screen
*/
let position = me.nearest(to: me.presentedView.frame.minY, inValues: [containerView.bounds.height, shortFormYPosition, longFormYPosition])

if position == longFormYPosition {
me.transition(to: .longForm)

} else if position == shortFormYPosition || me.presentable?.allowsDragToDismiss == false {
me.transition(to: .shortForm)

} else {
me.presentedViewController.dismiss(animated: true)
}
}

switch recognizer.state {
case .began, .changed:

Expand All @@ -499,6 +521,13 @@ private extension PanModalPresentationController {
presentable?.willTransition(to: .longForm)
}

case .cancelled,
.failed:
/**
When gesture is cancelled, transition to nearest position.
*/
transitionForPosition()

default:

/**
Expand Down Expand Up @@ -527,22 +556,7 @@ private extension PanModalPresentationController {
}

} else {

/**
The `containerView.bounds.height` is used to determine
how close the presented view is to the bottom of the screen
*/
let position = nearest(to: presentedView.frame.minY, inValues: [containerView.bounds.height, shortFormYPosition, longFormYPosition])

if position == longFormYPosition {
transition(to: .longForm)

} else if position == shortFormYPosition || presentable?.allowsDragToDismiss == false {
transition(to: .shortForm)

} else {
presentedViewController.dismiss(animated: true)
}
transitionForPosition()
}
}
}
Expand Down

0 comments on commit a164ce8

Please sign in to comment.