UILabel:在标签上使用userInteractionEnabled方法
-
03-07-2019 - |
题
我想知道是否有人在UILabel上使用了userInteractionEnabled方法来允许标签像按钮一样(或者只是触发一个方法)。任何帮助将不胜感激。干杯!
更新(4/30/09 @ 1:07pm)澄清:我有一个标准的InfoButton,在它旁边我想放置一个带有文字“settings”的标签。我希望标签的功能类似于按钮(翻转到设置屏幕。所以,基本上我需要将已经定义的showSettinsView绑定到“infoLabel”标签;用户点击infoButton或infoLabel和方法火了。
infoButton已经在运行,并且正在使用IBAction来触发该方法。我想知道如何连接标签以实现相同的方法。就这些。干杯!
解决方案
userInteractionEnabled
不是方法,而是属性。但我想你会想把它设置为 YES
,以允许事件进入 UIView
superview。
您可能想要做的是覆盖 UIView
的 touchesBegan:withEvent:
方法,该方法包含 UIButton
和 UILabel
子视图。
在此方法中,测试任何 UITouch
触摸是否属于 UILabel
的范围内。
也就是说, CGPoint
元素 [touch locationInView]
是否与 CGRect
元素 [infoLabel bounds]相交码>?查看函数
CGRectContainsPoint
来运行此测试。
如果是这样,那么启动一个 NSNotification
,它调用与 UIButton
相同的 IBAction
选择器。
其他提示
另一种解决方案可能是使用 UIButton
,其类型设置为custom,而不是 UILabel
。这样您的第二个按钮看起来就像 UILabel
,您可以将其配置为触发 showSettingsView
方法。
我同意Alex 2009年4月30日,如果你不想使用UIButton,我的提议只是一个补充。我解决它的方法是通过
创建一个UILabel- (id)initWithFrame:(CGRect)frame
{
label = [[UILabel alloc] initWithFrame: CGRectMake(5, 5, 20, 20)];
...
[label setUserInteractionEnabled: YES];
[self addSubview: label];
}
然后你可以覆盖触摸*方法,如touchesBegan,touchesEnded类似
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event {
UITouch *touch = [touches anyObject];
if([touch view] == label) {
NSLog(@"I've been touched");
}
}