UIScrollView 决定拖动和点击

发布于 2024-12-18 22:46:27 字数 660 浏览 1 评论 0原文

我试图确定 UIScrollView 内视图上的单个选项卡。问题是 UIScrollview 捕获所有手势。

到目前为止我尝试过的: 我在 UIScrollView 中覆盖以下方法:

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    UITouch *touch = [touches anyObject];
    if([touch tapCount]== 2) return YES;

    return NO;
}

这工作正常,我现在可以在 UIView 上到达 UITapGestureRecognize,不幸的是我只能检测双击,因为 [touch tapCount] == 1 总是被调用(拖动或缩放UIScrollView)。但实际上 UIScrollview 不需要“单击功能”

有没有办法在该方法内决定拖动(滚动或缩放)和单击?我找不到它..

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

提前致谢 法比

I am trying to determine a Single Tab on a View inside of a UIScrollView. The Problem is that The UIScrollview catches all the gestures.

What I tried so far:
I override the following method in my UIScrollView:

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    UITouch *touch = [touches anyObject];
    if([touch tapCount]== 2) return YES;

    return NO;
}

This works fine, I can now reach the UITapGestureRecognize on my UIView, unfortunately I can only detect double-taps because the [touch tapCount] == 1 is always beeing called (dragging or zooming in the UIScrollView). But actually the UIScrollview does not need the "Single-Tap-Function"

Is there a way to decide between a drag (Scroll or zoom) and a single Tap inside this method? I cant find it..

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

Thanks in advance
Fabi

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

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

发布评论

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

评论(3

你的背包 2024-12-25 22:46:27

听起来您只希望点击识别器在触摸不滚动滚动视图时成功。这非常简单,因为滚动视图使用平移手势识别器进行滚动。在 iOS 5 上,你可以这样做:

[self.tapRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];

如果你想支持旧版本的 iOS,你必须这样做:

for (UIGestureRecognizer *recognizer in self.scrollView.gestureRecognizers) {
    if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]])
        [self.tapRecognizer requireGestureRecognizerToFail:recognizer];

It sounds like you only want the tap recognizer to succeed if the touch doesn't scroll the scroll view. This is pretty easy because the scroll view uses a pan gesture recognizer for scrolling. On iOS 5, you can just do this:

[self.tapRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];

If you want to support older versions of iOS, you have to do this:

for (UIGestureRecognizer *recognizer in self.scrollView.gestureRecognizers) {
    if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]])
        [self.tapRecognizer requireGestureRecognizerToFail:recognizer];
梨涡少年 2024-12-25 22:46:27

touchesShouldBegin: 是 UIView 上的委托方法。它不是 UIGestureRecognizer 的一部分。

您需要创建一个 UITapGestureRecognizer,设置要触发识别器的点击次数并将其添加到您的视图中。

这是 UITapGestureRecognizer 的文档

touchesShouldBegin: is a delegate method on UIView. It is not part of a UIGestureRecognizer.

You'll need to create a UITapGestureRecognizer, set the number of taps you want to trigger the recognizer and add it to your view.

Here is the documentation for UITapGestureRecognizer

╰つ倒转 2024-12-25 22:46:27

我假设你的意思是“单击”——你的scrollView属于与你的控制器绑定的UIView。将点击手势识别器分配给该 UIView。它首先选择所有手势,然后传递它不感兴趣的手势。

I'm assuming you meant "single tap"--your scrollView belongs to the UIView tied to your controller. Assign the tap gesture recognizer to the that UIView. It gets first picks on all gestures and then passes down the ones it isn't interested in.

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