UITapGestureRecognizer 不执行任何操作

发布于 2024-11-16 21:22:16 字数 593 浏览 4 评论 0原文

我有一个应用程序,可以显示一页文本,可以点击按钮或在视图中滑动以在各个页面中前进或后退。容器视图附加了两个 UISwipeGestureRecognizer,用于向左滑动和向右滑动。这些手势没有任何问题。但现在我正在尝试将 UITapGestureRecognizer 添加到另一个视图中,提供类似于 iBooks 或 Kindle 应用程序的功能,点击左侧返回,点击右侧前进。我所做的任何事情都无法让手势启动。即使我将手势放在最上面的视图上并禁用其他手势,也没有任何迹象表明它会被触发。

视图控制器实现 UIGestureRecognizerDelegate,尽管我不需要实现委托方法。我确实尝试实现 shouldReceiveTouch: 但没有成功。

这是我创建并附加手势识别器的代码片段:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTurnPage:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];

I've got an app that displays a page of text with the ability to tap a button or swipe in a view to advance or retreat through various pages. The container view has two UISwipeGestureRecognizers attached, for swipe left and swipe right. No trouble with these gestures. But now I'm trying to add UITapGestureRecognizer to another view, providing an ability similar to iBooks or the Kindle app, tap the left to go back, the right to go forward. Nothing I do can get the gesture to fire. No hint that it is ever being triggered, even if I put the gesture on my topmost view and disable other gestures.

The view controller implements UIGestureRecognizerDelegate, though I've not needed to implement delegate methods. I did try implementing shouldReceiveTouch: without success.

Here's a snippet of code where I create and attach the gesture recognizer:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTurnPage:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

扛刀软妹 2024-11-23 21:22:16

在您要添加手势识别器的视图上尝试此操作:

view.userInteractionEnabled = YES;

Try this on the view you're adding the gesture recognizers:

view.userInteractionEnabled = YES;
半山落雨半山空 2024-11-23 21:22:16

您提到您已经尝试过此操作,但以防万一再次尝试。

尝试在标头中使用带有 的委托,然后设置委托:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTurnPage:)];
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];

然后实现此方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

然后使用调试器或将 NSLog 放入上述方法中并还可以tapGestureTurnPage: 查看是否正在调用这些方法。

You mention that you've tried this, but just in case go through it again.

Try using the delegate with <UIGestureRecognizerDelegate> in the header and then setting the delegate:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureTurnPage:)];
[self.view addGestureRecognizer:recognizer];
recognizer.delegate = self;
[recognizer release];

Then implement this method:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
       shouldReceiveTouch:(UITouch *)touch {
    return YES;
}

Then use a debugger or toss an NSLog into the above method and also tapGestureTurnPage: to see if these methods are being called.

淡紫姑娘! 2024-11-23 21:22:16

在初始化手势的地方添加这个:

recognizer.delegate = self;

并在“self”类中添加这个:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

这允许我在 UIWebView 上识别手势,并且 UIWebView 仍然会响应点击。 (我想要的 - 你可能不想要)

Add this where you initialize the gesture:

recognizer.delegate = self;

and add this in the "self" class:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

This allows me to have gestures recognized on the UIWebView, and the UIWebView will still respond to the taps. (which i wanted - you may not)

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