iOS UITapGestureRecognizer 有时不工作

标签 ios objective-c xcode uigesturerecognizer uitapgesturerecognizer

有时不会调用选择器方法“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/

相关文章:

ios - 回合制多人游戏 (Game Center, iOS) - 关闭 GKMatchmakerViewController 并呈现自己的用户界面来玩回合

objective-c - 执行 alloc 和 class_createInstance 有什么区别

objective-c - 如何在 Objective-C 中将本地 "variables"声明为 final?

C - Xcode 中的重定义错误

iOS PDF 生成 : How Do I Draw an MkMapView in PDF?

ios - 如何创建自定义 TextCell 和 TextRow,其中包含 UITextField 的子类?

ios - 如何知道推送的 View 何时停止移动。 UIKit 动态

ios - 具有完整图像背景溢出的 PageViewController

ios - CNCopySupportedInterfaces() 没有返回任何值,尽管 wifi 已连接

swift - 更改 ViewController MacOS Swift