iosuibuttoncore-animationcalayercatransformlayer

How can I make a CALayer clickable?


I am using CATransformLayer to create a bunch of layers and animate them with 3D perspective to make a cube (something like this)

CATransformLayer with sublayers

What's the best way of adding a UIButton to one of the sides to this cube? I would like to be able receive events and access the buttom from an IBOutlet.

I've tried creating a UIView with a button inside it into my XIB and inserting its layer into the CATransformLayer, it shows up but the button isn't clickable. I have a feeling this is because UIKit deals with the Touch/events side of things, the layers are just there to do the drawing.

(btw, I have seen this question, but feel this case is different enough to warrant a new post)


Solution

  • the root view thats hosting all the layers is getting all touches for the layer tree

    in there you gotta do

    CALayer *touchedLayer = [self.rootLayer hitTest:touches.anyObject.locationInWindow];
    

    from the comment: if you want to check during animations assk your presentation layer:

    CALayer *touchedLayer = [self.rootLayer.presentationLayer hitTest:touches.anyObject.locationInWindow];