Search code examples
iosuiviewuitouch

UITouch & UIEvents: fighting the framework?


Imagine a view with, say, 4 subviews, next to each other but non overlapping. Let's call them view#1 ... view#4

All 5 such views are my own UIView subclasses (yes, I've read: Event Handling as well as iOS Event Guide and this SO question and this one, not answered yet)

When the user touches one of them, UIKit "hiTests" it and delivers subsequent events to that view: view#1

Even when the finger goes outside view#1, over say view#3.

Even if this "drag" is now over view#3, view#1 still receives touchesMoved, but view#3 receives nothing.

I want view#3 to start replying to the touches. Maybe with a "touchedEntered" of my own, together with possibly a "touchesExited" on view#1.

How would I go about this?

I can see two approaches.

  1. side step the problem and do all the touch handling in the parent view whenever I detect a touchesMoved outside of view#1 bounds or,
  2. transfer to the parent view telling it to "redispatch". Not very clear how such redispatching would work, though.

For solution #2 where I am getting confused is not about the forwarding per se, but how to find the UIVIew I want to forward to. I can obviously loop through the parent subviews until I find one whose bounds/frame contain the touch, but I am wondering if I am missing something, that Apple would have already provided but I cannot relate to this problem.

Any idea?


Solution

  • I have done this, but I used CALayers instead of sub-UIViews. That way, there is no worries about the subviews catching/redispatching events to the parent UIView. You might not be able to do that, but it does simplify things. My solution tended to use CGRectContainsPoint() a lot.