Event handling for iOS – how hitTest:withEvent: and pointInside:withEvent: are related?

I think you are confusing subclassing with the view hierarchy. What the doc says is as follows. Say you have this view hierarchy. By hierarchy I’m not talking about class hierarchy, but views within views hierarchy, as follows:

+----------------------------+
|A                           |
|+--------+   +------------+ |
||B       |   |C           | |
||        |   |+----------+| |
|+--------+   ||D         || |
|             |+----------+| |
|             +------------+ |
+----------------------------+

Say you put your finger inside D. Here’s what will happen:

  1. hitTest:withEvent: is called on A, the top-most view of the view hierarchy.
  2. pointInside:withEvent: is called recursively on each view.
    1. pointInside:withEvent: is called on A, and returns YES
    2. pointInside:withEvent: is called on B, and returns NO
    3. pointInside:withEvent: is called on C, and returns YES
    4. pointInside:withEvent: is called on D, and returns YES
  3. On the views that returned YES, it will look down on the hierarchy to see the subview where the touch took place. In this case, from A, C and D, it will be D.
  4. D will be the hit-test view

Leave a Comment