简体   繁体   中英

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. This used to work in iOS6 it has suddenly stopped working in 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.

Thanks for the help!

UIImageView has userInteractionEnabled disabled by default. So you have to enable it manually.:

[imageToTap setUserInteractionEnabled:YES];

By default, user interaction is disabled on UIImageView. Try setting imageToTap.userInteractionEnabled = YES and see if that works for you. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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