Touch events on subclass of UIView as a subview of UIScrollView

touch, uiscrollview, uiview

Solution

There are several approaches you could try:

Try setting the below properties on the `UIScrollView`:

`scrollView.delaysContentTouches = NO;` `scrollView.canCancelContentTouches = NO;`

See similar SO questions/answers here, here.

Implement `hitTest:withEvent:`. See here, here.

Use a `UIGestureRecognizer`. See here, here.

I would personally recommend using a `UIGestureRecognizer`, but it depends on your specific situation (any of these options may work fine for you).

Problem

I have implemented my own custom subclass of `UIView` and overridden the `drawRect:` method. In my custom view I also want the handle touches, so I also overridden `touchesBegan`, `touchesMoved` and `touchesEnded`. This works fine but if the number of views on the screen increases then I have to use a `UIScrollView` as the root view of my `UIViewController`. Once my custom `UIView` becomes the subview of `UIScrollView`, then it does not receive the touch events. Even though I move my finger within my custom `UIView`, the scroll view gets scrolled (all my touch events go to the `UIScrollView`). How do I solve this problem?

Original source

Related problems