UITapGestureRecognizer 的问题

发布于 2024-11-06 06:35:00 字数 275 浏览 5 评论 0原文

我有一个主 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自找没趣 2024-11-13 06:35:00

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

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

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

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

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 示例你应该有一个很好的主意。

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

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

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

  2. 将所需的识别器类型从对象库拖放到所需的 UI 元素上。

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

  4. 你完成了!在此处输入图像描述

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:

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

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];

handler is the same:

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

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!

  1. Open your storyboard/nib.

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

  3. 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.

  4. You're done!enter image description here

初懵 2024-11-13 06:35:00

我认为您需要将手势识别器的委托设置为您想要处理点击的任何视图控制器。

gestureRecognizer.delegate = self;

例如。然后在标头中采用 UIGestureRecognizerDelegate 协议。

I think you need to set the delegate of the gesture recognizer to be whatever view controller you want to handle the taps.

gestureRecognizer.delegate = self;

for example. Then adopt the UIGestureRecognizerDelegate protocol in your header.

眼眸印温柔 2024-11-13 06:35:00

您可以选择UITapGestureRecognizer的接收者,它不必是实例化识别器的同一个类(即self)。如果您在 WelcomeViewConroller 中创建它,则可以选择任何子视图来接收事件:

[tapRecognizer addTarget:aSubview action:@selector(myMethod:)];

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 your WelcomeViewConroller, you can select any subview to receive the events:

[tapRecognizer addTarget:aSubview action:@selector(myMethod:)];
木槿暧夏七纪年 2024-11-13 06:35:00

我想你可能也遇到了我遇到的同样的问题。当您调用 [yourCustomView addGestureRecognizer:tapRecognizer];您需要通过 UIView * 引用,因此在您的示例中
尝试:

UIView *mySubView = yourCustomView; 
[mySubView addGestureRecognizer:tapRecognizer];

希望这有帮助。

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:

UIView *mySubView = yourCustomView; 
[mySubView addGestureRecognizer:tapRecognizer];

hope this helps.

踏月而来 2024-11-13 06:35:00

请检查view.userInteractionEnabled=true;

Please check view.userInteractionEnabled=true;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文