如果我使用 CALayer 绘制 UIButton 则不会触发选择器
大家好,我像这样创建了这个按钮:
_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事件不会发送到 (1) 隐藏、(2) alpha < 的控件。 0.1、(3) userInteraction=NO 等。由于您没有按钮图像、没有按钮标题、没有背景颜色、没有背景图像,因此它可能被视为透明(如 alpha < 0.1)并且没有获取事件。尝试添加图像、标题或背景颜色(在 UIView 中,而不是图层中)以查看是否存在问题。
编辑:尝试将背景颜色放入视图的背景颜色中,而不是图层的背景颜色中:
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.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.