Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28f38f4

Browse files
committed
Merge branch 'feature/issue_72'
2 parents bf53ea2 + c0b9d65 commit 28f38f4

8 files changed

+70
-13
lines changed

Example/OverlayContainer_Example/Present Overlay/ActivityControllerPresentationLikeViewController.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ class ActivityControllerPresentationLikeViewController: UIViewController,
3434
container.viewControllers = [ColoredViewController()]
3535
container.delegate = self
3636
container.moveOverlay(toNotchAt: Notch.medium.rawValue, animated: false)
37-
let root = UIViewController()
38-
root.addChild(container, in: root.view)
39-
root.transitioningDelegate = self
40-
root.modalPresentationStyle = .custom
41-
present(root, animated: true, completion: nil)
37+
container.transitioningDelegate = self
38+
container.modalPresentationStyle = .custom
39+
present(container, animated: true, completion: nil)
4240
}
4341

4442
// MARK: - UIViewControllerTransitioningDelegate
@@ -67,6 +65,10 @@ class ActivityControllerPresentationLikeViewController: UIViewController,
6765
return policy
6866
}
6967

68+
func overlayContainerSheetPresentationControllerShouldDismissOnTap(_ presentationController: OverlayContainerSheetPresentationController) -> Bool {
69+
return false
70+
}
71+
7072
// MARK: - OverlayTransitioningDelegate
7173

7274
func overlayTargetNotchPolicy(for overlayViewController: UIViewController) -> OverlayTranslationTargetNotchPolicy? {

Source/Classes/Internal/Extensions/UIViewController+Children.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ extension UIViewController {
2929
}
3030
return parent?.oc_findPresentationController(Controller.self)
3131
}
32+
33+
func oc_findChildren<Controller: UIViewController>(_ type: Controller.Type) -> [Controller] {
34+
if let controller = self as? Controller {
35+
return [controller]
36+
} else {
37+
return children.map { $0.oc_findChildren(type) }.flatMap { $0 }
38+
}
39+
}
3240
}

Source/Classes/Internal/OverlayTranslationGestureRecognizer.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ class OverlayTranslationGestureRecognizer: UIPanGestureRecognizer {
1111

1212
weak var drivingScrollView: UIScrollView?
1313

14-
var translationOffset: CGFloat = 0
15-
1614
private(set) var startingLocation: CGPoint = .zero
1715

1816
// MARK: - Public

Source/Classes/Internal/PanGestureOverlayTranslationDriver.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import UIKit
99

10-
class PanGestureOverlayTranslationDriver: NSObject, OverlayTranslationDriver {
10+
class PanGestureOverlayTranslationDriver: NSObject,
11+
OverlayTranslationDriver,
12+
UIGestureRecognizerDelegate {
1113

1214
private weak var translationController: OverlayTranslationController?
1315
private let panGestureRecognizer: OverlayTranslationGestureRecognizer
@@ -19,6 +21,7 @@ class PanGestureOverlayTranslationDriver: NSObject, OverlayTranslationDriver {
1921
self.translationController = translationController
2022
self.panGestureRecognizer = panGestureRecognizer
2123
super.init()
24+
panGestureRecognizer.delegate = self
2225
panGestureRecognizer.addTarget(self, action: #selector(overlayPanGestureAction(_:)))
2326
}
2427

@@ -28,6 +31,16 @@ class PanGestureOverlayTranslationDriver: NSObject, OverlayTranslationDriver {
2831
// no-op
2932
}
3033

34+
// MARK: - UIGestureRecognizerDelegate
35+
36+
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
37+
guard let view = gestureRecognizer.view,
38+
let gesture = gestureRecognizer as? OverlayTranslationGestureRecognizer else {
39+
return false
40+
}
41+
return translationController?.isDraggable(at: gesture.startingLocation, in: view) ?? false
42+
}
43+
3144
// MARK: - Action
3245

3346
@objc private func overlayPanGestureAction(_ sender: OverlayTranslationGestureRecognizer) {

Source/Classes/OverlayContainerPresentationController.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ import UIKit
1616
/// the `OverlayContainerSheetPresentationController` class to see if it can be adapted to your presentation behavior.
1717
open class OverlayContainerPresentationController: UIPresentationController {
1818

19+
// MARK: - Internal
20+
21+
open override func presentationTransitionWillBegin() {
22+
super.presentationTransitionWillBegin()
23+
findPresentedContainers().forEach { $0.overlayContainerPresentationTransitionWillBegin() }
24+
}
25+
26+
open override func dismissalTransitionDidEnd(_ completed: Bool) {
27+
super.dismissalTransitionDidEnd(completed)
28+
findPresentedContainers().forEach { $0.overlayContainerDismissalTransitionDidEnd() }
29+
}
30+
31+
// MARK: - Public
32+
1933
/// Tells the presentation controller when the user is about to start dragging the overlay view controller.
2034
///
2135
/// - parameter containerViewController: The container requesting this information.
@@ -65,4 +79,10 @@ open class OverlayContainerPresentationController: UIPresentationController {
6579
open func overlayContainerViewController(_ containerViewController: OverlayContainerViewController,
6680
willTranslateOverlay overlayViewController: UIViewController,
6781
transitionCoordinator: OverlayContainerTransitionCoordinator) {}
82+
83+
// MARK: - Private
84+
85+
private func findPresentedContainers() -> [OverlayContainerViewController] {
86+
presentedViewController.oc_findChildren(OverlayContainerViewController.self)
87+
}
6888
}

Source/Classes/OverlayContainerSheetPresentationController.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ open class OverlayContainerSheetPresentationController: OverlayContainerPresenta
8585
}
8686

8787
open override func presentationTransitionDidEnd(_ completed: Bool) {
88+
super.presentationTransitionDidEnd(completed)
8889
guard !completed else { return }
8990
dimmingView?.removeFromSuperview()
9091
tapGestureRecognizerView.removeFromSuperview()

Source/Classes/OverlayContainerTransitionCoordinator.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import UIKit
99

1010
/// A protocol that provides information about the current overlay translation.
1111
public protocol OverlayContainerTransitionContext {
12-
/// A Boolean value that indicates whether the user is current dragging the overlay.
12+
/// A Boolean value that indicates whether the user is currently dragging the overlay.
1313
var isDragging: Bool { get }
1414
/// The overlay velocity.
1515
var velocity: CGPoint { get }
@@ -54,7 +54,7 @@ public protocol OverlayContainerTransitionCoordinatorContext: OverlayContainerTr
5454
var targetTranslationHeight: CGFloat { get }
5555
}
5656

57-
/// A protocol that provides support for animations associated with a overlay translation.
57+
/// A protocol that provides support for animations associated with an overlay translation.
5858
///
5959
/// Do not adopt this procotol in your own classes. Use the one provided by the `OverlayContainerDelegate` to
6060
/// add any extra animations alongside the translation animations.

Source/Classes/OverlayContainerViewController.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ open class OverlayContainerViewController: UIViewController {
2020
/// `OverlayStyle` defines how the overlay view controller will be constrained in the container.
2121
public enum OverlayStyle {
2222
/// The overlay view controller will not be height-constrained. It will grow and shrink
23-
/// as the user drags them up and down.
23+
/// as the user drags it up and down.
2424
case flexibleHeight
2525
/// The overlay view controller will be constrained with a height equal to the highest notch.
26-
/// It will be fully visible only when the user has drag them up to this notch.
26+
/// It will be fully visible only when the user has drag it up to this notch.
2727
case rigid
2828
/// The overlay view controller will be constrained with a height greater or equal to the highest notch.
2929
/// Its height will be expanded if the overlay goes beyond the highest notch.
@@ -96,6 +96,10 @@ open class OverlayContainerViewController: UIViewController {
9696
private var translationController: HeightConstraintOverlayTranslationController?
9797
private var translationDrivers: [OverlayTranslationDriver] = []
9898

99+
// (gz) 2020-08-11 Uses to determine whether we can safely call `presentationController` or not.
100+
// See issue #72
101+
private var isPresentedInsideAnOverlayContainerPresentationController = false
102+
99103
// MARK: - Life Cycle
100104

101105
/// Creates an instance with the specified `style`.
@@ -153,6 +157,16 @@ open class OverlayContainerViewController: UIViewController {
153157
performDeferredTranslations()
154158
}
155159

160+
// MARK: - Internal
161+
162+
func overlayContainerPresentationTransitionWillBegin() {
163+
isPresentedInsideAnOverlayContainerPresentationController = true
164+
}
165+
166+
func overlayContainerDismissalTransitionDidEnd() {
167+
isPresentedInsideAnOverlayContainerPresentationController = false
168+
}
169+
156170
// MARK: - Public
157171

158172
/// Moves the overlay view controller to the specified notch.
@@ -311,7 +325,8 @@ open class OverlayContainerViewController: UIViewController {
311325
extension OverlayContainerViewController: HeightConstraintOverlayTranslationControllerDelegate {
312326

313327
private var overlayPresentationController: OverlayContainerPresentationController? {
314-
oc_findPresentationController(OverlayContainerPresentationController.self)
328+
guard isPresentedInsideAnOverlayContainerPresentationController else { return nil }
329+
return oc_findPresentationController(OverlayContainerPresentationController.self)
315330
}
316331

317332
// MARK: - HeightOverlayTranslationControllerDelegate

0 commit comments

Comments
 (0)