点击手势在uiimageview上无法识别

时间:2013-10-07 06:09:44

标签: objective-c uiimageview uitapgesturerecognizer ios6.1

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

分配点击手势:

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

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

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

//向uiimageview添加手势

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

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

但我的水龙头无法辨认。

任何人都能告诉我错误在哪里吗?

2 个答案:

答案 0 :(得分:108)

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

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

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

<强>更新

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

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

答案 1 :(得分:2)

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