如果我使用 CALayer 绘制 UIButton 则不会触发选择器

发布于 2024-08-30 21:50:44 字数 675 浏览 4 评论 0原文

大家好,我像这样创建了这个按钮:

_myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

    DLBadgeLayerDelegate *layerDelegate = [[DLBadgeLayerDelegate alloc] init];

    _myButton.frame = CGRectMake(0.0 , 0.0, 100.0, 100.0);
    [_myButton addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    _myButton.layer.backgroundColor = [[UIColor redColor] CGColor];

    _myButton.center = CGPointMake(10.0, 10.0);
    [self addSubview:_myButton];
    [_myButton release];

该按钮出现在屏幕上,但如果我点击它,什么也不会发生。如果我只是创建一个圆角矩形按钮,而不是在其图层上没有自定义绘图,或者如果我只是将图层代码行保留在外面,那么一切都会很好。您是否知道为什么我会出现这种行为,因为我想在按钮的图层中进行一些自定义绘图?

Hi everybody I create this button like this:

_myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

    DLBadgeLayerDelegate *layerDelegate = [[DLBadgeLayerDelegate alloc] init];

    _myButton.frame = CGRectMake(0.0 , 0.0, 100.0, 100.0);
    [_myButton addTarget:self action:@selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    _myButton.layer.backgroundColor = [[UIColor redColor] CGColor];

    _myButton.center = CGPointMake(10.0, 10.0);
    [self addSubview:_myButton];
    [_myButton release];

The button appears on the screen but if I tap on it nothing happens. If I just create a Rounded rect button instead with no custom drawing on its layer or if I just leave the layer line of code out all works well. Do you have any Idea why do I get this behaviour as I would like to do some custom drawing in the button's layer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰◇生如夏花灿烂 2024-09-06 21:50:44

事件不会发送到 (1) 隐藏、(2) alpha < 的控件。 0.1、(3) userInteraction=NO 等。由于您没有按钮图像、没有按钮标题、没有背景颜色、没有背景图像,因此它可能被视为透明(如 alpha < 0.1)并且没有获取事件。尝试添加图像、标题或背景颜色(在 UIView 中,而不是图层中)以查看是否存在问题。

编辑:尝试将背景颜色放入视图的背景颜色中,而不是图层的背景颜色中:

_myButton.backgroundColor = [UIColor redColor];

Events are not sent to a control that is (1) Hidden, (2) alpha < 0.1, (3) userInteraction=NO, etc. Since you have no button image, no button title, no background color, no background image, maybe it is being treated as transparent (like alpha < 0.1) and is not getting events. Try adding an image, title, or background color (in the UIView, not the layer) to see if that's the problem.

EDIT: Try putting the background color in the view's backgroundColor instead of in the layer's backgroundColor:

_myButton.backgroundColor = [UIColor redColor];
平定天下 2024-09-06 21:50:44

如果我忽略此行:

_myButton.layer.backgroundColor = [[UIColor redColor] CGColor];

事件会正确触发,但我仍然没有标题、alpha 等。我不认为是问题或解释,但我也会尝试这样做。

If I leave this line out:

_myButton.layer.backgroundColor = [[UIColor redColor] CGColor];

the event is triggered correctly and I still have no title, alpha, etc. I don't think that is the problem or explication, but I will try that way too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文