objective-c 在 uiimageview 上无法识别点击手势

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19218080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:04:41  来源:igfitidea点击:

tap gesture not recognized on uiimageview

objective-cuiimageviewuitapgesturerecognizerios6.1

提问by user2706770

I added two uiimageviews, one on another subview uiview(imageview1,imageview2). In the first view the top uiimageviewis hidden(imageview2) and in the second view the bottom imageviewis hidden(imageview1).

我添加了两个uiimageviews,一个在另一个subview uiview( imageview1,imageview2) 上。在第一个视图中,顶部uiimageview是隐藏的(imageview2),而在第二个视图中,底部imageview是隐藏的(imageview1)。

Allocating tap gesture:

分配点击手势:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];

Set user interaction for both uiimageview to YES.

将 uiimageview 的用户交互设置为 YES。

[singleTap setNumberOfTapsRequired:1];
[singleTap1 setNumberOfTapsRequired:1];

// adding gesture to uiimageview

// 向 uiimageview 添加手势

Add tap gesture recognizer and selector respectively.

分别添加点击手势识别器和选择器。

[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];

But my taps are not recognized.

但是我的水龙头无法识别。

Can any one tell me where the mistake is?

谁能告诉我错误在哪里?

回答by User 1531343

Try setting setUserInteractionEnabled:YESbefore adding gesture recognizer.

setUserInteractionEnabled:YES在添加手势识别器之前尝试设置。

[imageview1 setUserInteractionEnabled:YES]
[imageview2 setUserInteractionEnabled:YES]

[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];   

Update:

更新:

After the comment you have made I suggest you bring your views to the top before detecting the tap event. Because parent imageView is above and catches these taps.

在您发表评论之后,我建议您在检测点击事件之前将您的观点带到顶部。因为父 imageView 位于上方并捕获这些点击。

[yourparentview bringSubviewToFront:imageview1];
[yourparentview bringSubviewToFront:imageview2];

回答by n00bProgrammer

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
singleTap.delegate = self;
[imageview1 addGestureRecogniser:singleTap];
[singleTap1 release];

imageview1.userInteractionEnabled = YES; //disabled by default