Question

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)

Was it helpful?

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];

OTHER TIPS

Thanks to Daij for the hint, here's my solution - would appreciated any comments/improvements

- (IBAction)viewTapped:(UITapGestureRecognizer *)sender
{
    CGPoint location = [sender locationInView:sender.view];
    CALayer *layer = [sender.view.layer hitTest:location];

    BOOL contains = [self.btnPlay.layer.sublayers containsObject:layer];
    if(contains)
    {
        [self.btnPlay sendActionsForControlEvents:UIControlEventTouchUpInside];

    }


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top