From 007f9af3ebea3d01494f13859a8093e4c09fcdb8 Mon Sep 17 00:00:00 2001 From: Shin Yamamoto Date: Thu, 3 Sep 2020 23:07:33 +0900 Subject: [PATCH] Enable the removal interaction at any positions upon the conditions (#335) If a library consumer allows a panel projectable movement with the FloatingPanelBehavior object, the panel is able to invoke the removal interaction when the next moving position projected the momentum is hidden. --- Examples/Samples/Sources/ViewController.swift | 13 ++++++++++++- Framework/Sources/FloatingPanelCore.swift | 14 ++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Examples/Samples/Sources/ViewController.swift b/Examples/Samples/Sources/ViewController.swift index 4d9f7ecf..7700afb1 100644 --- a/Examples/Samples/Sources/ViewController.swift +++ b/Examples/Samples/Sources/ViewController.swift @@ -1355,15 +1355,26 @@ final class MultiPanelController: FloatingPanelController, FloatingPanelControll private final class FirstViewLayout: FloatingPanelLayout { let initialPosition: FloatingPanelPosition = .full - let supportedPositions: Set = [.full] + let supportedPositions: Set = [.full, .half] func insetFor(position: FloatingPanelPosition) -> CGFloat? { switch position { case .full: return 40.0 + case .half: return 200.0 default: return nil } } } + private final class FirstViewBehavior: FloatingPanelBehavior { + func shouldProjectMomentum(_ fpc: FloatingPanelController, for proposedTargetPosition: FloatingPanelPosition) -> Bool { + return true + } + } + + func floatingPanel(_ vc: FloatingPanelController, behaviorFor newCollection: UITraitCollection) -> FloatingPanelBehavior? { + return FirstViewBehavior() + } + func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? { return FirstViewLayout() } diff --git a/Framework/Sources/FloatingPanelCore.swift b/Framework/Sources/FloatingPanelCore.swift index 18bb25f1..deccea66 100644 --- a/Framework/Sources/FloatingPanelCore.swift +++ b/Framework/Sources/FloatingPanelCore.swift @@ -617,7 +617,7 @@ class FloatingPanelCore: NSObject, UIGestureRecognizerDelegate { endInteraction(for: targetPosition) - if isRemovalInteractionEnabled, isBottomState { + if isRemovalInteractionEnabled { let velocityVector: CGVector if distance == 0 { velocityVector = .zero @@ -627,12 +627,14 @@ class FloatingPanelCore: NSObject, UIGestureRecognizerDelegate { } // `velocityVector` will be replaced by just a velocity(not vector) when FloatingPanelRemovalInteraction will be added. if shouldStartRemovalAnimation(with: velocityVector), let vc = viewcontroller { - vc.delegate?.floatingPanelDidEndDraggingToRemove(vc, withVelocity: velocity) - let animationVector = CGVector(dx: abs(velocityVector.dx), dy: abs(velocityVector.dy)) - startRemovalAnimation(vc, with: animationVector) { [weak self] in - self?.finishRemovalAnimation() + if behavior.shouldProjectMomentum(vc, for: targetPosition) || isBottomState { + vc.delegate?.floatingPanelDidEndDraggingToRemove(vc, withVelocity: velocity) + let animationVector = CGVector(dx: abs(velocityVector.dx), dy: abs(velocityVector.dy)) + startRemovalAnimation(vc, with: animationVector) { [weak self] in + self?.finishRemovalAnimation() + } + return } - return } }