UITapGestureRecognizer 的问题
我有一个主 viewController,它称为 WelcomeViewController
。我有一个 UIView 子类,其中有一些与视图相关的内容。我想向该子类添加一个 UITapGestureRecognizer
。我只希望手势识别器确认该子视图内的点击。我该怎么做呢。我应该将 UITapGestureRecognizer
放在子类中还是应该将其放在 Welcome vc 中。提前致谢。
另外,我已经玩了很多次了,但似乎无法弄清楚。
I have a main viewController, it is called WelcomeViewController
. I have a UIView subclass and that has some view related stuff in it. I want to add a UITapGestureRecognizer
to that subclass. I only want the gesture recognizer to acknowledge taps inside that subview. How do I do that. Should I put the UITapGestureRecognizer
in the subclass or should I put it in the Welcome vc. Thanks in advance.
Also, I have played around with it a bunch and can't seem to figure it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这取决于您是否想要在自定义视图对象或视图控制器中处理点击。
如果在视图中,请将其添加到其
init
或其他适当的位置:如果在视图控制器中,请将其添加到
viewDidLoad
(或其他适当的位置):处理程序是相同:
查看 SimpleGestureRecognizers 示例你应该有一个很好的主意。
---- 更新于 10/1/2012----
对于那些喜欢使用 Storyboard/nib 的人来说,这非常简单!
打开您的故事板/笔尖。
将所需的识别器类型从对象库拖放到所需的 UI 元素上。
右键单击识别器对象,然后将其
选择器
连接到文件所有者中的 IBAction(通常是 UIViewController)。如果需要,还可以连接委托。你完成了!
It depends on whether you want to handle the tap in your custom view object or in the view controller.
If in the view, add this to it's
init
or other proper place:If in the view controller, add this in the
viewDidLoad
(or other proper place):handler is the same:
Take a look at the SimpleGestureRecognizers example and you should get a pretty good idea.
---- Updated 10/1/2012----
For those of you who like to use storyboard/nib, this is super simple!
Open your storyboard/nib.
Drag-b-drop the kind of recognizer you want from the Object Library onto the UI element you want.
Right-click on the recognizer object, then connect its
selector
to an IBAction in the File's Owner (usually an UIViewController.) If you need to, connect the delegate as well.You're done!
我认为您需要将手势识别器的委托设置为您想要处理点击的任何视图控制器。
例如。然后在标头中采用 UIGestureRecognizerDelegate 协议。
I think you need to set the delegate of the gesture recognizer to be whatever view controller you want to handle the taps.
for example. Then adopt the UIGestureRecognizerDelegate protocol in your header.
您可以选择
UITapGestureRecognizer
的接收者,它不必是实例化识别器的同一个类(即self
)。如果您在WelcomeViewConroller
中创建它,则可以选择任何子视图来接收事件:You can choose the receiver of the
UITapGestureRecognizer
, it doesn't have to be the same class that instantiated the recognizer (i.e.self
). If you create it in yourWelcomeViewConroller
, you can select any subview to receive the events:我想你可能也遇到了我遇到的同样的问题。当您调用 [yourCustomView addGestureRecognizer:tapRecognizer];您需要通过 UIView * 引用,因此在您的示例中
尝试:
希望这有帮助。
I think you may have the same problem I experienced. When you call [yourCustomView addGestureRecognizer:tapRecognizer]; you need to reference via a UIView * so in your example
try:
hope this helps.
请检查
view.userInteractionEnabled=true;
Please check
view.userInteractionEnabled=true;