试图添加UITapGestureRecognizer但不起作用

时间:2014-02-20 20:44:03

标签: ios ios7 uitapgesturerecognizer

我尝试在UILabel上设置一个简单的UITapGestureRecognizer,但它无法正常工作。似乎没有任何东西被识别,并且没有任何内容写入日志。

在从viewDidLoad

调用的方法中
  UILabel *miTN = [[UILabel alloc] initWithFrame:CGRectMake(300, 100, 150, 15)];
  [miTN setUserInteractionEnabled:YES];
  UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
  [miTN addGestureRecognizer: tapRecognizer];
  [miView addSubview:miTN];

   ... later

  - (void)tapRecognized:(id)sender
  {
    NSLog(@"that tap was recognized");
  }

此外,这是为响应异步网络调用而调用的。这会导致问题吗?还有其他一些可能导致问题的问题吗?我不确定调试的第一步是什么 - 我检查了Color Blended Layers以查看它们是否是ovrelapping但似乎不是。

2 个答案:

答案 0 :(得分:1)

您应该将UIGestureRecognizerDelegate添加到viewcontroller类的协议列表中

@interface TSViewController : UIViewController <UIGestureRecognizerDelegate>

并插入字符串:

tapRecognizer.delegate = self;

并替换

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];

答案 1 :(得分:0)

在Swift中,您必须通过以下方式启用此功能         yourLabel.isUserInteractionEnabled = true