是否有方法可以为添加到CAReplicatorLayer的视图层启用触摸事件
我正在向CAReplicatorLayer添加一个UIButton,我想让按钮的原始实例和复制实例都是可点击的。
到目前为止,我已经尝试将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");
}发布于 2015-08-28 16:26:56
您需要复制按钮本身,因为层不接收触摸事件。此外,您还需要将按钮添加到视图层次结构中,而不仅仅是图层。
https://stackoverflow.com/questions/32265824
复制相似问题