Iphone UIScrollView和点击手势检测子视图
我已将TapGestureRecognitor添加到我的self.view中:Iphone UIScrollView和点击手势检测子视图,iphone,uiscrollview,uigesturerecognizer,Iphone,Uiscrollview,Uigesturerecognizer,我已将TapGestureRecognitor添加到我的self.view中: tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; tap.numberOfTapsRequired = 1; tap.numberOfTouchesRequired = 1; [self.view addGestureRecognizer:tap];
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[self.view addGestureRecognizer:tap];
[tap release];
该视图包含一个带有图像和标签的UIScrollView。我想检测用户是否点击标签
- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer {
CGPoint pt = [gestureRecognizer locationInView:self.view];
UIView *v = [self.view hitTest:pt withEvent:nil];
if ([v isKindOfClass:[UILabel class]]) {
NSLog(@"label!");
return;
}
// else do other stuff if its not a label
但是我没有看到标签!在我的日志中 我认为这是因为
userInteractionEnabled
在UILabel
s上默认为NO
。试着打开它
编辑:这确实是一个猜测,但只是为了确认,Apple文档位于[UIView hitTest:withEvent:][/code>状态:
此方法将忽略隐藏、禁用用户交互或alpha级别小于0.01的视图对象
您的子视图(例如标签本身)实际上隐藏了底层视图中的用户交互
为什么不在标签上添加手势识别器呢。
或者,您可能希望对标签使用UIButton
或
如果不想确定触摸了哪个标签,可以在所有标签的顶部添加一个不可见视图(空视图,既不是隐藏视图,也不是alpha=0的视图),然后将手势识别器添加到这些标签上 谢谢!我还修改了上面的代码,将UIScrollView用于hitTest和locationInView