我尝试在 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");
}
另外,这是响应异步网络调用而调用的。这可能是导致问题的原因吗?是否有其他问题可能导致问题?我不太确定调试的第一步是什么 - 我检查了颜色混合图层以查看它们是否重叠但看起来不是。
您应该将 UIGestureRecognizerDelegate 添加到您的 View Controller 类的协议(protocol)列表中
@interface TSViewController : UIViewController
并插入字符串:
tapRecognizer.delegate = self;
并替换
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
ĐẾN
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
Tôi là một lập trình viên xuất sắc, rất giỏi!