首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAReplicatorLayer和触摸事件

CAReplicatorLayer和触摸事件
EN

Stack Overflow用户
提问于 2015-08-28 15:42:57
回答 1查看 83关注 0票数 0

是否有方法可以为添加到CAReplicatorLayer的视图层启用触摸事件

我正在向CAReplicatorLayer添加一个UIButton,我想让按钮的原始实例和复制实例都是可点击的。

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

UIViewController

代码语言:javascript
复制
- (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");
}
EN

回答 1

Stack Overflow用户

发布于 2015-08-28 16:26:56

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32265824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档