How to detect touch on screen in custom UIView?

266 Views Asked by At

I want to watch in my UIView subclass for user touches on the screen. Especially there are some additional conditions:

  • the touch should not be catch by my view (for example, if user taps some button or scrolls something that action should be accepted)
  • there can be multiple views which are watching for touch and they should not conflict with each other
  • the approach should be as general as possible (so we can catch taps on tabbar or navigation buttons)

I can suppose to add specific fullscreen view with customized hitTest: method. This view has interface to add/remove delegates for watching for touches. But this method is "hard" in coding terms and I would like to find more elegant and simpler solution.

1

There are 1 best solutions below

1
On

you can write touchesBegan in that UIView subclass.

Because if it had any subview like button or scrollView, they would be first responders ,and touches on scrollView wont fire touchesBegan of your UIView subclass.

And it wont conflict with other touch listeners unless the listener subView is explicitly passing that touch to your Subclass.