CAReplicatorLayer and touch events

105 Views Asked by At

Is there a way to enable touch events for view layers added to a CAReplicatorLayer?

I'm adding a UIButton to the CAReplicatorLayer, I'd like to make both the original and duplicated instances of the button to be tappable.

So far I've tried forwarding UIView -hitTest:withEvent: and -touchesXXX:withEvent: methods to the button, nothing has worked for me yet (if this is even possible).

From the UIViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];

    static UIButton *button;    //  Temp hack to keep a ref to the button
    button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(30, 100, 60, 44);
    [button setTitle:@"Button" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

    CAReplicatorLayer *layer = [CAReplicatorLayer layer];
    layer.frame = self.view.bounds;
    layer.instanceCount = 2;
    layer.instanceTransform = CATransform3DMakeTranslation(70, 0, 0);
    [layer addSublayer:button.layer];
    [self.view.layer addSublayer:layer];
}

- (void)buttonTapped:(id)sender
{
    NSLog(@"Tapped");
}
1

There are 1 best solutions below

1
John Tracid On

You need to copy button itself because layers not receiving touch events. Also you need to add button to your view hierarchy, not only layers.