Skip to content

Commit

Permalink
Fixed gesture issues
Browse files Browse the repository at this point in the history
  • Loading branch information
luannguyenkhoa-agilityio committed Sep 10, 2017
1 parent f117119 commit 0c8099b
Showing 1 changed file with 70 additions and 57 deletions.
127 changes: 70 additions & 57 deletions LNSideMenu/Classes/LNSideMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import UIKit

public final class LNSideMenu: NSObject, UIGestureRecognizerDelegate {
public final class LNSideMenu: NSObject {

public typealias Completion = () -> ()

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

0 comments on commit 0c8099b

Please sign in to comment.