有时不会调用选择器方法“tagClickAtIndex”。
UILabel* label = [[UILabel alloc] init];
label.userInteractionEnabled = YES;
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tagClickAtIndex:)];
[label addGestureRecognizer:tapGesture];
-(void)tagClickAtIndex:(UITapGestureRecognizer*)gesture
{
NSMutableDictionary *mutDict = [[[NSMutableDictionary alloc]initWithDictionary:[_tagArray objectAtIndex:gesture.view.tag]] mutableCopy];
[mutDict setValue:[NSNumber numberWithLong:gesture.view.tag] forKey:@"index"];
[self.delegate tagClickAtIndex:mutDict];
}
最佳答案
你忘了设置label
的frame,所以它只定义了可点击区域
label.frame = CGRectMake(0,0,200,30)
[yourMainview addSubView:label]
如果需要可以设置
tapGesture.numberOfTapsRequired = 1;
关于iOS UITapGestureRecognizer 有时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38845486/