iphone – UITapGestureRecognizer的问题

语言: CN / TW / HK

此外,我已经玩了一堆,似乎无法搞清楚.

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

如果在视图中,将其添加到它的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的人来说,这非常简单!

>打开你的故事板/笔尖.

>将您想要的识别器类型从对象库拖放到所需的UI元素上.

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

>你完成了!

翻译自:https://stackoverflow.com/questions/5954934/issue-with-a-uitapgesturerecognizer

分享到: