1

I'm looking for a way to pass touches from a clear ViewController that is on top of another ViewController. I came across the following post already:

How to ignore touch events and pass them to another subview's UIControl objects?

I followed their tips but couldn't get it to work unfortunately.

I got the following view structure from top to bottom:

VideoViewController (note: clear ViewController can see the buttons underneath)
| passThroughView (note: UIView that is using the PassThroughView subclass)
| collectionView
CameraViewController
| controlContainer
| | recordButton

When tapping outside the collectionview and on the recordbutton which is behind the passThroughView it should execute a new record. Problem is that with the following code I can't make that happen:

class PassThroughView: UIView {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let hitView = super.hitTest(point, with: event)!
        let cameraVC = CameraViewController()

        if hitView == self {
            return cameraVC.recordButton
        }

        return hitView
    }
}

As soon as my passThroughView gets tapped I get a print in the if statement but it doesn't return my touch to the other ViewControllers button.

So what am I doing wrong here?

Community
  • 1
  • 1

1 Answers1

0

I would prefer to use a TapGestureRecognizer and then to decide what to do in certain coordinates:

Inside viewDidLoad of your topmost viewController:

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.didRecognizeTapGesture(gesture:)))        
self.view.addGestureRecognizer(tapGesture)

Add a method inside the same controller:

func didRecognizeTapGesture(gesture: UITapGestureRecognizer)
{
    let point = gesture.location(in: gesture.view)        
    if(gesture.state == UIGestureRecognizerState.ended) {
        let desiredFrame = CGRect(...) //e.g the portion of screen, where you need to make exceptional touch event
        if(desiredFrame.contains(point)) {
            print("... Caught a gesture")
        }
    }
}
pedrouan
  • 12,762
  • 3
  • 58
  • 74
  • I was aware of this trick but the problem is that when I would be using this, it takes out my button tap effect and I would have to simulate everything. Isn't there any way on targeting the recordbutton in the other ViewController? –  Sep 03 '16 at 16:36
  • 1
    I think this can handle your situation, to be able catch touch event of a button: http://stackoverflow.com/a/3348532/661022 – pedrouan Sep 03 '16 at 18:57
  • I appreciate the effort, but unfortunately that is not what I'm looking for. This will add a tapgesturerecognizer, which I don't even have in my case. I need a viewcontroller to ignore my touches and pass them through to the viewcontroller below it. Not a tapgesturerecognizer. Still tho I need to maintain interactivity on my CollectionView –  Sep 04 '16 at 16:58
  • http://stackoverflow.com/questions/34660095/pass-touches-through-a-uiviewcontroller I think this says it all, sadly haha. –  Sep 04 '16 at 17:02
  • Great searching skills. That's why I suggest own solution. I hope you find better one. – pedrouan Sep 04 '16 at 17:03
  • Yea will mark your answer as correct. Since it will do the trick, but not in the way I would like to! Thanks for your help anyway. I'll figure it out one way or another. –  Sep 04 '16 at 17:07
  • Thank you for your cooperation as well. – pedrouan Sep 04 '16 at 17:12