1

I have the following case. parentView has it's own gestureRecognizerAand has a subview subView which has it's own UITapGestureRecognizer. Is there any way to tell parentView that it should pass the touch events recognized in gestureRecognizerA to subView if these touch events are in subView's bounds?

gestureRecognizerA is very specific. It is a custom gesture recognizer for recognizing a circlular motion. This recognition should happen on all areas of parentView. However, when that same gesture recognizer recognizes a tap, it should pass that tap to subView.

enter image description here

WalterBeiter
  • 2,021
  • 3
  • 23
  • 48

2 Answers2

2

You can easily identify the points of tap. As for example you have a tap gesture in parent class as:

let tapGR = UITapGestureRecognizer(target: self, action: #selector(tapped))
view.addGestureRecognizer(tapGR)

@objc func tapped(gr:UITapGestureRecognizer) {
    let loc:CGPoint = gr.location(in: gr.view)  
    //insert your touch based code here
}

Inside the tapped method you can identify the location where tap happened, so after checking bounds of the subview with location of tap you can verify is the tap happened inside the bounds of subview or not.

Rahim Khalid
  • 469
  • 6
  • 15
  • well, this is indeed a way of doing it, but I want to pass the touch events to the subView. Not calculate it's bounds. – WalterBeiter Mar 04 '19 at 10:40
1

It seems like you just want both of those gesture recognizers to work simultaneously. Just implement UIGestureRecognizerDelegate for your parentView and make it tapGestureRecognizer's and gestureRecognizerA's delegate. Then implement an optional method there:

// MARK: - UIGestureRecognizerDelegate

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) {
    return true
}

That might be able to detect a tap in subView even while doing a circular motion within parentView.

UPDATE: When using gesture recognizers, "forwarding touches" would be to simply calling a method of another recognizer. Just put a recognizer which is doing the forwarding as its parameter.

For instance, tapGestureRecognizer fires viewWasTapped(_ sender: UITapGestureRecognizer) when a tap is detected. Now, when your gestureRecognizerA wants to forward its events to tapGestureRecognizer, it simply does so by calling:

subView.viewWasTapped(self.gestureRecognizerA)

With an obvious change to the method itself:

func viewWasTapped(_ sender: UIGestureRecognizer) {
    // ...
}

This works for UITapGestureRecognizer. The sender can be any other UIGestureRecognizer and you'd still have almost all the information to resolve a tap gesture there.

xius
  • 596
  • 1
  • 6
  • 20
  • not exactly. I want to have full control over when I want to delegate touch events to the other view. – WalterBeiter Mar 05 '19 at 07:24
  • 1
    Well, you could call a method which is now linked with tapGestureRecognizer. Just make its parameter be of "UIGestureRecognizer" class and pass gestureRecognizerA there. For all intends and purposes, you'd be "forwarding touches" in the world of gesture recognizers. – xius Mar 05 '19 at 07:31