使用UITapGestureRecognizer时出现问题 [英] Issue with a UITapGestureRecognizer

查看:123
本文介绍了使用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.

推荐答案

这取决于您是要在自定义视图对象中还是在视图控制器中处理点击。

It depends on whether you want to handle the tap in your custom view object or in the view controller.

如果在视图中,将其添加到 init 或其他适当位置:

If in the view, add this to it's init or other proper place:

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

如果在视图控制器中,请在 viewDidLoad (或其他适当位置):

If in the view controller, add this in the viewDidLoad (or other proper place):

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[yourCustomView addGestureRecognizer:tapRecognizer];
[tapRecognizer release];

处理程序是相同的:

- (void)handleTap:(UITapGestureRecognizer*)recognizer
{
    // Do Your thing. 
    if (recognizer.state == UIGestureRecognizerStateEnded)
    {
    }
}

查看 SimpleGestureRecognizers 例子,你应该得到一个非常好的主意。

Take a look at the SimpleGestureRecognizers example and you should get a pretty good idea.

----更新于10/1/2012 ----

对于那些喜欢使用storyboard / nib的人来说,这非常简单!

For those of you who like to use storyboard/nib, this is super simple!


  1. 打开你的故事板/笔尖。

  1. Open your storyboard/nib.

将你想要的识别器类型从对象库拖放到UI元素上你想要的。

Drag-b-drop the kind of recognizer you want from the Object Library onto the UI element you want.

右键单击识别器对象,然后将其选择器连接到IBAction中文件的所有者(通常是UIViewController。)如果需要,也可以连接代理。

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.

你已经完成了!

这篇关于使用UITapGestureRecognizer时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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