简体   繁体   English

如何判断UILabel是否被触摸?

[英]How can I determine if a UILabel was touched?

I am trying to determine if a UILabel was touched and if so do something. 我试图确定UILabel是否被触及,如果有的话。 Give .. 给..

.
.
.
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];
.
.
.    

Then I write the callback. 然后我写回调。

 - (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];
  }

The problem is right now, no matter where I touch in the View the Url is being opened. 现在的问题是,无论我在视图中触摸的位置是打开的。 How do I determine if only just my label was touched? 如何确定是否仅触摸了我的标签?

If you add the label to the class, you can do a hit-test on your view in the touch event with: 如果您将标签添加到课程中,您可以在触摸事件中对视图进行点击测试:

- (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] ...];
    ...
  }
}

Also, don't forget to release the URL you allocate (otherwise you are leaking). 另外,不要忘记释放您分配的URL(否则您将泄露)。

You can do this without overriding touchesBegan. 您可以在不覆盖touchesBegan的情况下执行此操作。 Use gesture recognizers. 使用手势识别器。

UILabel *label = ...;
label.userInteractionEnabled = YES;
UITapGestureRecognizer *recognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)] autorelease];

[label addGestureRecognizer:recognizer];

- (void)tapAction {
  NSLog(@"tap performed");
}

i think the best way for handle is to set flag for each uilabel part and then give the flag number from the code, 我认为最好的处理方法是为每个uilabel部分设置标志,然后从代码中给出标志号,

label.userInteractionEnabled = YES;


-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [touches anyobject];

    if(touch.view.tag == uurflagnumber)
    NSlog(@"touched");
} 

Eugene's solution works but you must have "user interaction enabled" ticked in your NIB file. Eugene的解决方案有效但您必须在NIB文件中勾选“启用用户交互”。 Or as Mahyar has with the label.userInteractionEnabled = YES. 或者像Mahyar的label.userInteractionEnabled = YES。

Here is my solution as I had two seperate labels that I wanted to capture touches from. 这是我的解决方案,因为我有两个单独的标签,我想从中捕捉触摸。 And I have "user interaction enabled" ticked in my nib file. 我在我的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];

I hope this helps. 我希望这有帮助。

You could also add an invisible (not text or image) UIButton as a subview the same size as your label. 您还可以将不可见(不是文本或图像) UIButton为与标签大小相同的子视图。 This has the added benefit of not capturing other types of touches, such as a touch on another area of the screen that accidentally slides onto your label and then lifts up. 这样做的另一个好处是不会捕捉其他类型的触摸,例如触摸屏幕上另一个意外滑入标签然后抬起的区域。 It may also be considered cleaner code. 它也可以被认为是更清晰的代码。

Another late answer ... 另一个迟到的答案

Like @Kevin Sylvestre suggests, you can test for the touch over a UILabel by overriding -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event in your view controller. 就像@Kevin Sylvestre建议的那样,你可以通过覆盖来测试UILabel上的触摸-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event视图控制器中的-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

However, rather than testing if the touch is located with the rect of the UILabel, I find it easier to test if the touch was on the 'view' of UILabel (remember UILabel inherits from UIView). 然而,我没有测试触摸是否位于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
    }
}

This technique can easily be applied to other interface objects that inherit from UIView. 此技术可以轻松应用于从UIView继承的其他接口对象。

Also, it is also necessary for the UILabel to have 'user interaction enabled' to react to the touch event. 此外,UILabel还必须具有“启用用户交互”以对触摸事件作出反应。 This can be done in Interface Builder (Attributes Inspector > View > User Interaction Enabled) or programatically - eg: self.myLabel.userInteractionEnabled = YES; 这可以在Interface Builder中完成(Attributes Inspector> View> User Interaction Enabled)或以编程方式完成 - 例如: self.myLabel.userInteractionEnabled = YES;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM