[英]How can I determine if a UILabel was touched?
我试图确定UILabel是否被触及,如果有的话。 给..
.
.
.
UILabel * site = [[UILabel alloc] initWithFrame:CGRectMake(0, 185, 320, 30)];
site.text = [retriever.plistDict valueForKey:@"url"];
site.textAlignment =UITextAlignmentCenter;
site.backgroundColor = [UIColor clearColor];
site.textColor = [UIColor whiteColor];
site.userInteractionEnabled = YES;
[theBgView addSubview:site];
[site release];
.
.
.
然后我写回调。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
retriever = [PListRetriever sharedInstance];
CGPoint pt = [[touches anyObject] locationInView: self];
NSURL *target = [[NSURL alloc] initWithString:[retriever.plistDict valueForKey:@"url"]];
[[UIApplication sharedApplication] openURL:target];
}
现在的问题是,无论我在视图中触摸的位置是打开的。 如何确定是否仅触摸了我的标签?
如果您将标签添加到课程中,您可以在触摸事件中对视图进行点击测试:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([self.site frame], [touch locationInView:self.view]))
{
NSURL *target = [[NSURL alloc] ...];
...
}
}
另外,不要忘记释放您分配的URL(否则您将泄露)。
您可以在不覆盖touchesBegan的情况下执行此操作。 使用手势识别器。
UILabel *label = ...;
label.userInteractionEnabled = YES;
UITapGestureRecognizer *recognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)] autorelease];
[label addGestureRecognizer:recognizer];
- (void)tapAction {
NSLog(@"tap performed");
}
我认为最好的处理方法是为每个uilabel部分设置标志,然后从代码中给出标志号,
label.userInteractionEnabled = YES;
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyobject];
if(touch.view.tag == uurflagnumber)
NSlog(@"touched");
}
Eugene的解决方案有效但您必须在NIB文件中勾选“启用用户交互”。 或者像Mahyar的label.userInteractionEnabled = YES。
这是我的解决方案,因为我有两个单独的标签,我想从中捕捉触摸。 我在我的nib文件中勾选了“启用了用户交互”。
UITapGestureRecognizer *theSpeedTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
[speedLabel addGestureRecognizer:theSpeedTapped];
UITapGestureRecognizer *theDirectionTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
[directionLabel addGestureRecognizer:theDirectionTapped];
我希望这有帮助。
您还可以将不可见(不是文本或图像) UIButton
为与标签大小相同的子视图。 这样做的另一个好处是不会捕捉其他类型的触摸,例如触摸屏幕上另一个意外滑入标签然后抬起的区域。 它也可以被认为是更清晰的代码。
另一个迟到的答案
就像@Kevin Sylvestre
建议的那样,你可以通过覆盖来测试UILabel上的触摸-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
视图控制器中的-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
。
然而,我没有测试触摸是否位于UILabel的矩形,我发现更容易测试触摸是否在UILabel的“视图”上(请记住UILabel继承自UIView)。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSSet *touchedViews = [touches valueForKeyPath:@"view"];
if ([touchedViews containsObject:self.myLabel]) {
// do something
}
}
此技术可以轻松应用于从UIView继承的其他接口对象。
此外,UILabel还必须具有“启用用户交互”以对触摸事件作出反应。 这可以在Interface Builder中完成(Attributes Inspector> View> User Interaction Enabled)或以编程方式完成 - 例如: self.myLabel.userInteractionEnabled = YES;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.