Search code examples
objective-ccore-animationcalayerhittest

CALayer hitTest with sublayers


I have a structure composed on a root layer and 2 CALayer subclass. These 2 layers contain a layer too.

Here a scheme:

ROOT LAYER 
    |
    |------- LAYER A
    |           |---------BG
    |
    |
    |--------LAYER B
                |---------BG

If I call the hitTest method on the ROOT LAYER it returns the most inner layer into the hierarchy. So if user clicks over LAYER A I get the BG of LAYER A.

//In this example hitResult will contains the BG of LAYER A or the BG of LAYER B
CALayer *hitResult = [rootLayer hitTest:point)]; 

How can I stop the responder chain and get directly LAYER A or LAYER B from a HitTest sent to ROOT LAYER ?


Solution

  • If these layers are your own CALayer subclasses then you can override hitTest: or containsPoint: to do your own logic there.

    Overriding containsPoint: and returning NO in your background layers will stop them from being returned from their superlayers hitTest: implementation.

    Alternatively you could override hitTest: in Layer A and Layer B and return self if they contain the point.