如何在具有目标c中的某个标记的特定标签上应用UITapGestureRecognizer

时间:2017-05-09 05:55:33

标签: tags uilabel uitapgesturerecognizer

我在一个循环中有4个标签,我想在每个标签上单独应用UITapGestureRecognizer以执行某些操作如果我想在标签2的标签上应用任何帮助,我怎么能得到这些东西呢。

for(int i = 0 ; i < 4; i++){
  label = [[UILabel alloc] initWithFrame:rect1];
    label.tag = LABEL_TAG+i;

    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor grayColor];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont fontWithName:@"Verdana-Bold" size:17.0];
    label.text = @"someText";
    [label sizeToFit];
     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotTapped:)];
    [self viewWithTag:LABEL_TAG+2 addGestureRecognizer:tap];

1 个答案:

答案 0 :(得分:0)

You only need to add UITapGestureRecognizer on each label.

for(UIlabel *label in cell.contentViews.subviews) {
    if(label.tag == 1) {
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [tap setNumberOfTapsRequired:1];
        [label addGestureRecognizer:tap];
    } }