From 0c8099b98147938020fd2650686b338bdd1d5631 Mon Sep 17 00:00:00 2001 From: Luan Nguyen Date: Sun, 10 Sep 2017 12:21:47 +0700 Subject: [PATCH] Fixed gesture issues --- LNSideMenu/Classes/LNSideMenu.swift | 127 +++++++++++++++------------- 1 file changed, 70 insertions(+), 57 deletions(-) diff --git a/LNSideMenu/Classes/LNSideMenu.swift b/LNSideMenu/Classes/LNSideMenu.swift index 26a86ce..15361e4 100644 --- a/LNSideMenu/Classes/LNSideMenu.swift +++ b/LNSideMenu/Classes/LNSideMenu.swift @@ -8,7 +8,7 @@ import UIKit -public final class LNSideMenu: NSObject, UIGestureRecognizerDelegate { +public final class LNSideMenu: NSObject { public typealias Completion = () -> () @@ -159,6 +159,7 @@ public final class LNSideMenu: NSObject, UIGestureRecognizerDelegate { let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture(gesture:))) tapGesture.numberOfTapsRequired = 1 tapGesture.numberOfTouchesRequired = 1 + tapGesture.delegate = self sideMenuContainerView.addGestureRecognizer(tapGesture) } @@ -266,62 +267,6 @@ public final class LNSideMenu: NSObject, UIGestureRecognizerDelegate { } } - public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { - print("completed: \(animationCompleted)") - // Disable gesture if dynamic animator has not ended animation yet - if !dynamicAnimatorEnded { - return false - } - // Disable gesture until the toggle menu animation is completed - if !animationCompleted { - return false - } - // Such as pan gesture, kill menu scrolling whenever user swipes on view - if !isCustomMenu { - menuViewController?.sideMenuView.killScrolling() - } - if let shouldOpen = delegate?.sideMenuShouldOpenSideMenu?() , !shouldOpen { - return false - } - - if let gestureRecognizer = gestureRecognizer as? UISwipeGestureRecognizer { - if !allowLeftSwipe && gestureRecognizer.direction == .left { - return false - } - - if !allowRightSwipe && gestureRecognizer.direction == .right { - return false - } - } else if gestureRecognizer == panGesture { - if !allowPanGesture { - return false - } - - let touchPosition = gestureRecognizer.location(ofTouch: 0, in: sourceView) - if position == .left { - if isMenuOpen && touchPosition.x < menuWidth { - return true - } - if touchPosition.x < kGestureXPoint { - return true - } - } else { - if isMenuOpen && touchPosition.x > sourceView.frame.width - menuWidth { - return true - } - if touchPosition.x > sourceView.frame.width - kGestureXPoint { - return true - } - } - return false - } - return true - } - - public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { - return true - } - internal func handleGesture(_ gesture: UISwipeGestureRecognizer) { // Toggle side menu by swipe gesture direction cacheEnableDynamic = false @@ -605,3 +550,71 @@ extension LNSideMenu: UIDynamicAnimatorDelegate { dispatch_group.enter() } } + +extension LNSideMenu: UIGestureRecognizerDelegate { + + public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { + print("completed: \(animationCompleted)") + // Disable gesture if dynamic animator has not ended animation yet + if !dynamicAnimatorEnded { + return false + } + // Disable gesture until the toggle menu animation is completed + if !animationCompleted { + return false + } + // Such as pan gesture, kill menu scrolling whenever user swipes on view + if !isCustomMenu { + menuViewController?.sideMenuView.killScrolling() + } + if let shouldOpen = delegate?.sideMenuShouldOpenSideMenu?() , !shouldOpen { + return false + } + + if let gestureRecognizer = gestureRecognizer as? UISwipeGestureRecognizer { + if !allowLeftSwipe && gestureRecognizer.direction == .left { + return false + } + + if !allowRightSwipe && gestureRecognizer.direction == .right { + return false + } + } else if gestureRecognizer == panGesture { + if !allowPanGesture { + return false + } + + let touchPosition = gestureRecognizer.location(ofTouch: 0, in: sourceView) + if position == .left { + if isMenuOpen && touchPosition.x < menuWidth { + return true + } + if touchPosition.x < kGestureXPoint { + return true + } + } else { + if isMenuOpen && touchPosition.x > sourceView.frame.width - menuWidth { + return true + } + if touchPosition.x > sourceView.frame.width - kGestureXPoint { + return true + } + } + return false + } + return true + } + + public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool { + return true + } + + public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { + if gestureRecognizer is UITapGestureRecognizer, tapOutsideToDismiss { + if let menu = customMenu?.view, let touchView = touch.view, touchView.isDescendant(of: menu) { + return false + } + } + return true + } +}