2

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?

Rahul Mane
  • 1,005
  • 18
  • 33
user2217812
  • 37
  • 1
  • 6

2 Answers2

7

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

Community
  • 1
  • 1
Steph Sharp
  • 11,462
  • 5
  • 44
  • 81
0

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.

Community
  • 1
  • 1
Andrei Stanescu
  • 6,353
  • 4
  • 35
  • 64