How can I determine if a UILabel was touched?

前端 未结 6 1367
天命终不由人
天命终不由人 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).

提交回复
热议问题