How can I determine if a UILabel was touched?

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

提交回复
热议问题