user2217812
user2217812

Reputation: 37

Touch events on subclass of UIView as a subview of UIScrollView

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?

Upvotes: 2

Views: 5184

Answers (2)

Steph Sharp
Steph Sharp

Reputation: 11682

There are several approaches you could try:

  1. Try setting the below properties on the UIScrollView:

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

    See similar SO questions/answers here, here.

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

  3. 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).

Upvotes: 7

Andrei Stanescu
Andrei Stanescu

Reputation: 6383

Have a look at this response from another question: https://stackoverflow.com/a/4629821/193254

You'll have to subclass the scrollview too, and implement that hitTest: method.

Upvotes: 0

Related Questions