Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions DrawerView/DrawerPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ extension UIViewController: DrawerPresenter {
public class DrawerPresentationController: UIPresentationController {

private let drawerView: DrawerView

private var presentationDelegate: DrawerPresentationDelegate?
private var isDismissing = false

init(presentedViewController: UIViewController,
presenting presentingViewController: UIViewController?,
Expand Down Expand Up @@ -76,6 +76,8 @@ public class DrawerPresentationController: UIPresentationController {

public override func dismissalTransitionWillBegin() {
super.dismissalTransitionWillBegin()
isDismissing = true

// Make callbacks backwards compatible
if let callback = presentationDelegate?.drawerDismissalWillBegin(for:in:) {
callback(presentedViewController, drawerView)
Expand All @@ -86,13 +88,8 @@ public class DrawerPresentationController: UIPresentationController {

public override func dismissalTransitionDidEnd(_ completed: Bool) {
super.dismissalTransitionDidEnd(completed)
isDismissing = false

// Clean up the drawer for reuse.
presentedViewController.view.removeFromSuperview()
presentedViewController.removeFromParent()
drawerView.removeFromSuperview()

// Make callbacks backwards compatible
if let callback = presentationDelegate?.drawerDismissalDidEnd(for:in:completed:) {
callback(presentedViewController, drawerView, completed)
} else {
Expand All @@ -103,10 +100,6 @@ public class DrawerPresentationController: UIPresentationController {
override public var shouldRemovePresentersView: Bool {
return false
}

override public func containerViewWillLayoutSubviews() {
super.containerViewWillLayoutSubviews()
}
}

@objc public protocol DrawerPresentationDelegate {
Expand All @@ -130,7 +123,8 @@ public class DrawerPresentationController: UIPresentationController {
extension DrawerPresentationController: DrawerViewDelegate {

public func drawer(_ drawerView: DrawerView, willTransitionFrom startPosition: DrawerPosition, to targetPosition: DrawerPosition) {
if targetPosition == .closed {
// Only trigger dismiss if we're not already dismissing to avoid recursion
if targetPosition == .closed && !isDismissing {
presentedViewController.dismiss(animated: true)
}
}
Expand Down Expand Up @@ -170,6 +164,7 @@ extension DrawerPresentationManager: UIViewControllerTransitioningDelegate {
public final class DrawerPresentationAnimator: NSObject {

let presentation: PresentationType
private let animationDuration: TimeInterval = 0.3 // Standard iOS animation duration

enum PresentationType {
case present
Expand All @@ -186,25 +181,31 @@ extension DrawerPresentationAnimator: UIViewControllerAnimatedTransitioning {
public func transitionDuration(
using transitionContext: UIViewControllerContextTransitioning?
) -> TimeInterval {
return 0.0
return animationDuration
}

public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

switch presentation {
case .present:
guard let drawerView = transitionContext.view(forKey: .to) as? DrawerView else {
transitionContext.completeTransition(false)
return
}

drawerView.setPosition(.open, animated: true) { finished in
transitionContext.completeTransition(finished)
}
transitionContext.completeTransition(finished)
}
case .dismiss:
guard let drawerView = transitionContext.view(forKey: .from) as? DrawerView else {
transitionContext.completeTransition(false)
return
}

let originalVisibilityBehavior = drawerView.contentVisibilityBehavior
drawerView.contentVisibilityBehavior = .never

drawerView.setPosition(.closed, animated: true) { finished in
drawerView.contentVisibilityBehavior = originalVisibilityBehavior
transitionContext.completeTransition(finished)
}
}
Expand Down
2 changes: 1 addition & 1 deletion DrawerView/DrawerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ private struct ChildScrollViewInfo {
}

func embed(view: UIView) {
view.backgroundColor = .clear
// view.backgroundColor = .clear
view.frame = self.bounds
view.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(view)
Expand Down
8 changes: 4 additions & 4 deletions Example/DrawerViewExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22154" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="23504" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22130"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="23506"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="Stack View standard spacing" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -211,7 +211,7 @@
<constraint firstAttribute="trailing" secondItem="com-Ya-LsP" secondAttribute="trailing" constant="20" id="HMD-C4-HQX"/>
<constraint firstAttribute="trailing" secondItem="uQc-Da-g9m" secondAttribute="trailing" constant="16" id="I5o-QI-hRy"/>
<constraint firstItem="uQc-Da-g9m" firstAttribute="leading" secondItem="A0d-xS-Dle" secondAttribute="leading" constant="16" id="f3F-Xr-zYx"/>
<constraint firstItem="uQc-Da-g9m" firstAttribute="top" secondItem="QP1-4T-vBO" secondAttribute="topMargin" constant="80" id="ncf-2F-Vyz"/>
<constraint firstItem="uQc-Da-g9m" firstAttribute="top" secondItem="QP1-4T-vBO" secondAttribute="top" constant="100" id="ncf-2F-Vyz"/>
<constraint firstItem="com-Ya-LsP" firstAttribute="leading" relation="greaterThanOrEqual" secondItem="A0d-xS-Dle" secondAttribute="leading" constant="20" id="vwH-J4-76C"/>
<constraint firstItem="Wev-iY-Wa9" firstAttribute="centerY" secondItem="com-Ya-LsP" secondAttribute="centerY" id="wQs-EA-Fhw"/>
</constraints>
Expand All @@ -222,7 +222,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="AU3-g5-75i" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1976.8" y="133.5832083958021"/>
<point key="canvasLocation" x="1976.8" y="133.13343328335833"/>
</scene>
</scenes>
</document>
8 changes: 4 additions & 4 deletions Example/DrawerViewExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ class ViewController: UIViewController {
}
}

let drawerPresentation = DrawerPresentationManager()
let settingsPresentation = DrawerPresentationManager()

private func presentDrawer() {
let viewController = self.storyboard!.instantiateViewController(withIdentifier: "ModalPresentationViewController") as! ModalPresentationViewController
drawerPresentation.drawer.openHeightBehavior = .fitting
viewController.transitioningDelegate = drawerPresentation
let viewController = UIViewController()
// settingsPresentation.drawer.openHeightBehavior = .fitting
viewController.transitioningDelegate = settingsPresentation
viewController.modalPresentationStyle = .custom
self.present(viewController, animated: true, completion: nil)
}
Expand Down