UIScrollView 只能用一根手指滚动

时间:2023-02-16
本文介绍了UIScrollView 只能用一根手指滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS7 &iOS8

iOS7 & iOS8

我需要在 UIScrollview 中禁用两指或三指滚动.

I need to disable 2 or three fingers scrolling in UIScrollview.

我试过了:

[self.scrollView.panGestureRecognizer setMaximumNumberOfTouches:1];
[self.scrollView.panGestureRecognizer setMinimumNumberOfTouches:1];

但它没有效果.仍然可以用两根手指滚动.

But it has no effect. It is still possible to scroll with 2 fingers.

如果我尝试将 max 和 min 设置为 2.一个手指滚动被禁用但 3 个手指滚动可能:(

If i tried to set max and min to 2. One finger scrolling was disabled but 3 fingers scrolling possible :(

我也试过了,但没有成功:

I tried this too, but without success:

for (UIGestureRecognizer* pan in self.scrollView.gestureRecognizers) {
        OTTNSLog(@"touches: %ld", (unsigned long)pan.numberOfTouches);
        if ([pan isKindOfClass:[UIPanGestureRecognizer class]])
        {
            UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) pan;
            mpanGR.minimumNumberOfTouches = 1;
            mpanGR.maximumNumberOfTouches = 1;

        }

        if ([pan isKindOfClass:[UISwipeGestureRecognizer class]])
        {
            UISwipeGestureRecognizer *mswipeGR = (UISwipeGestureRecognizer *) pan;
            mswipeGR.numberOfTouchesRequired = 1;
        }

    }

有人知道,它是如何解决这个问题的吗?

Does anybody know, how it solve this ?

谢谢.

推荐答案

问题:

UIPanGestureRecognizerUIScrollView 的底层时 - 不幸的是,这也会影响 UIPageViewController - maximumNumberOfTouches 未按预期运行,minimumNumberOfTouches 但是总是正确地限制下限.

When the UIPanGestureRecognizer is underlying a UIScrollView - which unfortunately does also effect UIPageViewController - the maximumNumberOfTouches is not behaving as expected, the minimumNumberOfTouches however always limits the lower end correctly.

在监控这些参数时,它们会报告正确的值——它们似乎在做自己的工作——只是 UIScrollView 本身不尊重它们并忽略它们的设置!

When monitoring these parameters they report back correct values - they seem to do their job - it's just that UIScrollView itself doesn't honor them and ignores their settings!

解决方案:

minimumNumberOfTouches 设置为所需的值,例如1 和 - 非常重要 - maximumNumberOfTouches 到 2 !!!

Set the minimumNumberOfTouches to the desired value e.g. 1 and - very importantly - the maximumNumberOfTouches to 2 !!!

myScrollView.panGestureRecognizer.minimumNumberOfTouches = 1;
myScrollView.panGestureRecognizer.maximumNumberOfTouches = 2;

在滚动视图的@interface 声明中符合 UIGestureRecognizerDelegate 协议.您不必为 UIScrollView 设置 panGestureRecognizer.delegate !!!委托已经设置,因为 UIScrollView 需要是它自己的 pan/pinchGestureRecognizer 的委托.

Conform to the UIGestureRecognizerDelegate protocol in your scrollView's @interface declaration. You don't have to set the panGestureRecognizer.delegate for a UIScrollView!!! The delegate is already set because UIScrollView requires to be the delegate of its own pan/pinchGestureRecognizer.

然后实现UIGestureRecognizer委托方法:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    NSLog(@"%d", gestureRecognizer.numberOfTouches);
    NSLog(@"%@", gestureRecognizer.description);

        if (gestureRecognizer == self.panGestureRecognizer) {
            if (gestureRecognizer.numberOfTouches > 1) {
                return NO;
            } else {
                return YES;
            }
        } else {
            return YES;
        }
    }
}

<小时>

更安全的版本:

如果您有一个自定义的 scrollView 类并且想要非常安全,您还可以再添加一行代码来消除与其他 scrollView 的歧义:

If you have a custom scrollView class and wanna be on the VERY safe side you can also add one more line of code to disambiguate against other scrollViews:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    NSLog(@"%d", gestureRecognizer.numberOfTouches);
    NSLog(@"%@", gestureRecognizer.description);

    if ([gestureRecognizer.view isMemberOfClass:[MY_CustomcrollView class]]) {
        if (gestureRecognizer == self.panGestureRecognizer) {
            if (gestureRecognizer.numberOfTouches > 1) {
                return NO;
            } else {
                return YES;
            }
        } else {
            return YES;
        }
    } else {
        return YES;
    }
}

<小时>

附加信息:

NSLog 告诉您触摸的次数.如果您将 minmax 设置为相同的值(如上例中的 1),if-loop 永远不会被触发... ;-)

The NSLogs tell you the number of touches. If you set both the min and max to the same value (like 1 in the example above) the if-loop would never be triggered... ;-)

这就是为什么 maximumNumberOfTouches 必须至少为 minimumNumberOfTouches + 1

That is why maximumNumberOfTouches has to be at least minimumNumberOfTouches + 1

panGestureRecognizer.minimumNumberOfTouches = 1;
panGestureRecognizer.maximumNumberOfTouches = minimumNumberOfTouches + 1;

<小时>

极客部分:

for (UIView *view in self.view.subviews) {
    if ([view isKindOfClass:[UIScrollView class]]) {
        NSLog(@"myPageViewController - SCROLLVIEW GESTURE RECOGNIZERS: %@", view.gestureRecognizers.description);
        ((UIPanGestureRecognizer *)view.gestureRecognizers[1]).minimumNumberOfTouches = 1;
        ((UIPanGestureRecognizer *)view.gestureRecognizers[1]).maximumNumberOfTouches = 2;
    }
}

这是访问负责 UIPageViewController 分页的底层滚动视图的方法.将此代码放在例如UIPageViewController(自身)的viewDidLoad:.

如果您根本无法访问滚动视图 - 就像动态 UITableViewCell 中的 UIPageViewController 在运行时发生创建和单元重用并且没有插座可以在其 contentViews 上设置 - 在 UIScrollView 上放置一个类别并在那里覆盖委托方法.不过要小心!这会影响您的应用程序中的每个滚动视图 - 所以要像上面的更安全"示例中那样进行适当的自省(类检查)...... ;-)

If you don't have access to the scrollView at all - like for a UIPageViewController in a dynamic UITableViewCell where creation and cell reuse happens at runtime and no outlets can be set on its contentViews - put a category on UIScrollView and override the delegate method there. But be careful! This effects every scrollView in your application - so do proper introspection (class-checking) like in my 'EVEN SAFER' example above... ;-)

旁注:

不幸的是,同样的技巧不适用于 UIScrollView 上的 pinchGestureRecognizer 因为它不公开 min/maxNumberOfTouches 属性.当被监控时,它总是报告 2 次触摸(你显然需要捏) - 所以它的内部 min/maxNumberOfTouches 似乎都设置为2 - 即使 UIScrollView 不遵守其自己的设置并继续愉快地用任意数量的手指(超过 2 个)捏合.所以没有办法将捏合限制在有限数量的手指上......

Unfortunately the same trick doesn't work with the pinchGestureRecognizer on UIScrollView because it doesn't expose a min/maxNumberOfTouches property. When monitored it always reports 2 touches (which you obviously need to pinch) - so its internal min/maxNumberOfTouches seem to have been set both to 2 - even if UIScrollView isn't honoring its own settings and keeps happily pinching with any numbers of fingers (more than 2). So there is no way to restrict pinching to a limited amount of fingers...

这篇关于UIScrollView 只能用一根手指滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:在 UIScrollView 中使用 UITouch 下一篇:如何在 iphone 应用程序开发中的导航栏上添加 1

相关文章

最新文章