简体   繁体   English

UITapGestureRecognizer不响应iOS7中添加的子视图

[英]UITapGestureRecognizer does not respond to added subview in iOS7

In iOS7, gesture recognizers on programmatically added subviews do not seem to be triggering, however when programmatically adding gesture recognizers to views that were added via my storyboard interface, gesture recognizers trigger no problem. 在iOS7中,似乎没有触发以编程方式添加的子视图上的手势识别器,但是当以编程方式将手势识别器添加到通过我的故事板界面添加的视图时,手势识别器不会触发任何问题。 This used to work in iOS6 it has suddenly stopped working in iOS7. 这曾经在iOS6中起作用,但突然停止在iOS7中起作用。 What am I doing wrong or what have I missed? 我做错了什么或错过了什么?

UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomethingWhenTapped:)];

UIImageView *imageToTap = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Transparent" ofType:@"png"]]];
imageToTap.frame = CGRectMake(0, 0, 100, 100);
imageToTap.backgroundColor = [UIColor redColor];
[imageToTap addGestureRecognizer:tapGestureRecognizer];

[self.view addSubview:imageToTap];

EDIT: 编辑:

Although I forgot to add the userInteractionEnabled property to my example (it is set in my real code) the below suggestions to add it made me realize my real issue is an odd frame/bounds issue I'm seeing in Landscape mode. 尽管我忘记在示例中添加userInteractionEnabled属性(它是在我的真实代码中设置的),但以下添加建议却使我意识到我的真​​正问题是在横向模式下遇到的奇怪的帧/边界问题。

Thanks for the help! 谢谢您的帮助!

UIImageView has userInteractionEnabled disabled by default. UIImageView默认情况下禁用了userInteractionEnabled So you have to enable it manually.: 因此,您必须手动启用它。

[imageToTap setUserInteractionEnabled:YES];

By default, user interaction is disabled on UIImageView. 默认情况下,在UIImageView上禁用用户交互。 Try setting imageToTap.userInteractionEnabled = YES and see if that works for you. 尝试将imageToTap.userInteractionEnabled = YES设置imageToTap.userInteractionEnabled = YES ,看看是否适合您。 Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM