您可以将 UIGestureRecognizer 附加到多个视图吗? [英] Can you attach a UIGestureRecognizer to multiple views?

查看:26
本文介绍了您可以将 UIGestureRecognizer 附加到多个视图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTapTap:)];
[self.view1 addGestureRecognizer:tapGesture];
[self.view2 addGestureRecognizer:tapGesture];
[tapGesture release];

在上面的代码中,只能识别 view2 上的点击.如果我注释掉第三行然后点击 view1 被识别.如果我是对的并且您只能使用一次手势识别器,我不确定这是一个错误还是它只需要更多文档.

In the above code only taps on view2 are recognized. If I comment out the third line then taps on view1 are recognized. If I'm right and you can only use a gesture recognizer once, I'm not sure if this is a bug or it just needs some more documentation.

推荐答案

UIGestureRecognizer 用于单个视图.我同意文档是参差不齐的.UIGestureRecognizer 有一个 view 属性就可以了:

A UIGestureRecognizer is to be used with a single view. I agree the documentation is spotty. That UIGestureRecognizer has a single view property gives it away:

查看

手势识别器附加到的视图.(只读)

The view the gesture recognizer is attached to. (read-only)

@property(nonatomic, readonly) UIView *view

@property(nonatomic, readonly) UIView *view

讨论您将手势识别器附加(或添加)到 UIView 对象使用 addGestureRecognizer:方法.

Discussion You attach (or add) a gesture recognizer to a UIView object using the addGestureRecognizer: method.

这篇关于您可以将 UIGestureRecognizer 附加到多个视图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆