How can I determine if a UILabel was touched?

前端 未结 6 1210
天命终不由人
天命终不由人 2020-12-25 08:39

I am trying to determine if a UILabel was touched and if so do something. Give ..

.
.
.
UILabel * site = [[UILabel alloc] initWithFrame:CGRectMake(0, 185, 32         


        
相关标签:
6条回答
  • 2020-12-25 09:08

    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).

    0 讨论(0)
  • 2020-12-25 09:10

    You can do this without overriding 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");
    }
    
    0 讨论(0)
  • 2020-12-25 09:12

    Eugene's solution works but you must have "user interaction enabled" ticked in your NIB file. Or as Mahyar has with the 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.

        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.

    0 讨论(0)
  • 2020-12-25 09:12

    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.

    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).

    - (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.

    Also, it is also necessary for the UILabel to have 'user interaction enabled' to react to the touch event. This can be done in Interface Builder (Attributes Inspector > View > User Interaction Enabled) or programatically - eg: self.myLabel.userInteractionEnabled = YES;

    0 讨论(0)
  • 2020-12-25 09:22

    i think the best way for handle is to set flag for each uilabel part and then give the flag number from the code,

    label.userInteractionEnabled = YES;
    
    
    -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
        UITouch *touch = [touches anyobject];
    
        if(touch.view.tag == uurflagnumber)
        NSlog(@"touched");
    } 
    
    0 讨论(0)
  • 2020-12-25 09:22

    You could also add an invisible (not text or image) UIButton as a subview the same size as your label. 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.

    0 讨论(0)
提交回复
热议问题