From a07a08568e38d0f11450cfa01ecb80cf13328926 Mon Sep 17 00:00:00 2001 From: superk589 Date: Thu, 2 Feb 2017 11:11:13 +0800 Subject: [PATCH] add property shouldRequireFailureOfNavigationPopGesture --- README.md | 5 +++++ ZKDrawerController.podspec | 2 +- ZKDrawerController/AppDelegate.swift | 2 +- .../Core/ZKDrawerController.swift | 18 ++++++------------ .../Core/ZKDrawerScrollView.swift | 15 ++++++++++++++- ZKDrawerController/ViewController.swift | 2 +- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 4235a66..cb8c2d6 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,11 @@ drawerController.shadowWidth = 5 drawerController.gestureRecognizerWidth = 40 ``` +### should require failure of navigation pop gesture, default true (if setting false, showing left drawer gesture will have higher priority) +```swift +drawerController.shouldRequireFailureOfNavigationPopGesture = true +``` + ### set the side or main controller dynamically ```swift // set or replace diff --git a/ZKDrawerController.podspec b/ZKDrawerController.podspec index 4051458..47a060f 100644 --- a/ZKDrawerController.podspec +++ b/ZKDrawerController.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "ZKDrawerController" - s.version = "0.0.8" + s.version = "0.0.9" s.summary = "An iOS drawer controller in swift." s.description = <<-DESC A light-weighted iOS drawer controller in swift. diff --git a/ZKDrawerController/AppDelegate.swift b/ZKDrawerController/AppDelegate.swift index 30e11e6..d922e58 100644 --- a/ZKDrawerController/AppDelegate.swift +++ b/ZKDrawerController/AppDelegate.swift @@ -31,7 +31,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { let left = UIViewController() left.view.backgroundColor = UIColor.clear - let drawer = ZKDrawerController.init(main: nav, right: right) + let drawer = ZKDrawerController.init(main: nav, left: left) drawer.mainScale = 0.8 drawer.containerView.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1) drawer.drawerStyle = .plain diff --git a/ZKDrawerController/Core/ZKDrawerController.swift b/ZKDrawerController/Core/ZKDrawerController.swift index 42b9ec5..ce28d2d 100644 --- a/ZKDrawerController/Core/ZKDrawerController.swift +++ b/ZKDrawerController/Core/ZKDrawerController.swift @@ -182,17 +182,13 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate { fatalError("init(coder:) has not been implemented") } - /// 解决左侧抽屉划出手势和导航控制器手势冲突的问题, 默认在ZKDrawerController初始化时会进行调用 - /// - /// - Parameter vc: 导航控制器 或者包含了导航控制器的父控制器 - open func resolveGestureConflict(vc: UIViewController) { - if let nav = vc as? UINavigationController { - if let gesture = nav.interactivePopGestureRecognizer { - containerView.panGestureRecognizer.require(toFail: gesture) - } + /// default true, 解决左侧抽屉划出手势和导航控制器手势冲突的问题 + open var shouldRequireFailureOfNavigationPopGesture: Bool { + get { + return containerView.shouldRequireFailureOfNavigationPopGesture } - for childVC in vc.childViewControllers { - resolveGestureConflict(vc: childVC) + set { + containerView.shouldRequireFailureOfNavigationPopGesture = newValue } } @@ -251,8 +247,6 @@ open class ZKDrawerController: UIViewController, ZKDrawerCoverViewDelegate { make.right.equalTo(-rightWidth) make.width.equalToSuperview() } - // 如果主视图控制器包含导航控制器, 导航手势失败才可以执行左侧菜单手势 - resolveGestureConflict(vc: vc) } func removeOldVC(vc: UIViewController?) { diff --git a/ZKDrawerController/Core/ZKDrawerScrollView.swift b/ZKDrawerController/Core/ZKDrawerScrollView.swift index 76c1f16..3cf0e58 100644 --- a/ZKDrawerController/Core/ZKDrawerScrollView.swift +++ b/ZKDrawerController/Core/ZKDrawerScrollView.swift @@ -9,7 +9,7 @@ import UIKit import SnapKit -public class ZKDrawerScrollView: UIScrollView { +public class ZKDrawerScrollView: UIScrollView, UIGestureRecognizerDelegate { var rightWidth: CGFloat = 0 @@ -21,6 +21,8 @@ public class ZKDrawerScrollView: UIScrollView { var gestureRecognizerWidth:CGFloat = 40 + var shouldRequireFailureOfNavigationPopGesture: Bool = true + public override init(frame: CGRect) { super.init(frame: frame) @@ -28,6 +30,7 @@ public class ZKDrawerScrollView: UIScrollView { self.showsHorizontalScrollIndicator = false self.bounces = false self.isPagingEnabled = true + self.panGestureRecognizer.delegate = self } public required init?(coder aDecoder: NSCoder) { @@ -65,6 +68,16 @@ public class ZKDrawerScrollView: UIScrollView { } return false } + + public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool { + if otherGestureRecognizer is UIScreenEdgePanGestureRecognizer { + return shouldRequireFailureOfNavigationPopGesture + } else { + return false + } + } + + // public override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) { // super.setContentOffset(contentOffset, animated: animated) // if animated && contentOffset != self.contentOffset { diff --git a/ZKDrawerController/ViewController.swift b/ZKDrawerController/ViewController.swift index bacfb85..965033c 100644 --- a/ZKDrawerController/ViewController.swift +++ b/ZKDrawerController/ViewController.swift @@ -17,7 +17,7 @@ class ViewController: UIViewController { view.backgroundColor = UIColor.blue //navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "show", style: .plain, target: self, action: #selector(showRight)) - navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "show", style: .plain, target: self, action: #selector(showLeft)) + // navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "show", style: .plain, target: self, action: #selector(showLeft)) // Do any additional setup after loading the view, typically from a nib. let button = UIButton.init(frame: CGRect.init(x: 200, y: 200, width: 150, height: 100))