0

有没有办法为添加到的视图层启用触摸事件CAReplicatorLayer

我正在向 中添加一个UIButtonCAReplicatorLayer我想让按钮的原始实例和重复实例都可以点击。

到目前为止,我已经尝试了按钮的转发UIView -hitTest:withEvent:-touchesXXX:withEvent:方法,但对我来说还没有任何效果(如果这可能的话)。

来自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");
}
4

1 回答 1

0

您需要复制按钮本身,因为图层不接收触摸事件。您还需要将按钮添加到视图层次结构中,而不仅仅是图层。

于 2015-08-28T08:26:55.797 回答