How to Exclude gesture for subview?

时间:2019-03-06 11:37:26

标签: ios swift uigesturerecognizer gesture swrevealviewcontroller

i am working with an app which takes signature in image view. and im using SWRevealViewController for side menu. my app looks like this.

enter image description here

     class CaptureSignature: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate {

im writing below code to show side menu

     override func viewDidLoad() {
            super.viewDidLoad()
    let revealViewController = self.revealViewController()
            menuBtn.addTarget(revealViewController, action: #selector(SWRevealViewController.rightRevealToggle(_:)), for: .touchUpInside)
            revealViewController?.panGestureRecognizer()?.isEnabled = true
    }

now my problem is, while im swiping on image view from right to left, instead of drawing a line, side menu is opening.(because i have enabled panGesture in SWRevealViewController).

What i dont want is, i dont want to enable swipe gesture on imageView to show menu bar when i swipe right. i wrote the below func but its not working(even it is not entering into method when i put breakpoints in it)

       func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {


        if let touchedView = touch.view, touchedView.isDescendant(of: imageView) {
            print("touched in image view")
            return false
        }
        return false

    }

can any one help me please.

5 个答案:

答案 0 :(得分:0)

您是否尝试过在UIImageView上将isUserInteractionEnabled设置为true

答案 1 :(得分:0)

如果要禁用特定视图的手势。

self.<yourview>.isUserInteractionEnabled = false

答案 2 :(得分:0)

您可以在“显示”控制器上调整属性。

revealController.draggableBorderWidth = # Your Value #

如果触摸开始小于此值,它将限制显示控制器开始平移手势。 就像您的情况一样,如果您的图像视图x位置为50。因此,您可以将此属性设置为40。当用户触摸位置小于40时,比平移手势更有效。默认情况下,其值为0,这就是开始平移手势的原因从任何地方。

答案 3 :(得分:0)

SWRevealViewController 提供了此功能。写吧 revealViewController().panGestureRecognizer().isEnabled = false 在控制器中要禁用该功能以打开菜单。

答案 4 :(得分:0)

您可以使用以下方法忽略特定区域的滑动手势。

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool

,您仍然可以致电touchesMoved来签名。这是示例代码。

func addSwipeGesture() {
   let leftSwipe = UISwipeGestureRecognizer(target: self, action: 
      #selector(handleSwipes(_:)))
   leftSwipe.delegate = self
   let rightSwipe = UISwipeGestureRecognizer(target: self, action: 
      #selector(handleSwipes(_:)))
   rightSwipe.delegate = self

   leftSwipe.direction = .left
   rightSwipe.direction = .right

   self.view.addGestureRecognizer(leftSwipe)
   self.view.addGestureRecognizer(rightSwipe)
}

@objc func handleSwipes(_ sender:UISwipeGestureRecognizer) {

   if (sender.direction == .left) {
     print("Swipe Left")
   }

   if (sender.direction == .right) {
     print("Swipe Right")
   }
}

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive 
touch: UITouch) -> Bool {
   return !self.imgView.bounds.contains(touch.location(in: self.imgView))
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
   print("moving")
}

对于上述代码段,图像视图(即imgView

的滑动操作将被忽略)