在 UIScrollView 中禁用 2 根手指滚动

发布于 2024-12-17 05:44:54 字数 819 浏览 1 评论 0原文

我想在 UIScrollView 中禁用两指滚动。
我对其进行了子类化,并使用以下代码调整了其内置手势识别器:

for (UIGestureRecognizer *mgestureRecognizer in scroller.gestureRecognizers) {     
    if ([mgestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) mgestureRecognizer;
        mpanGR.minimumNumberOfTouches = 1; 
        mpanGR.maximumNumberOfTouches = 1;

    }

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

出于某种原因,maximumNumberOfTouches 似乎不起作用。我仍然可以用一两根手指滚动。如果我将这两个属性都更改为 2,我可以成功禁用单指滚动并需要两次触摸。

有什么想法吗?

I'd like to disable two-finger scrolling in my UIScrollView.
I subclassed it and tweaked its built-in gesture recognizers with the following code:

for (UIGestureRecognizer *mgestureRecognizer in scroller.gestureRecognizers) {     
    if ([mgestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
    {
        UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) mgestureRecognizer;
        mpanGR.minimumNumberOfTouches = 1; 
        mpanGR.maximumNumberOfTouches = 1;

    }

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

For some reason, maximumNumberOfTouches does not seem to work. I can still scroll with one or two fingers. If I change both properties to 2, I can successfully disable one-finger scrolling and require two touches.

Any ideas?

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

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

发布评论

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

评论(4

白况 2024-12-24 05:44:54

我意识到这是一个旧线程,但我花了很长时间才弄清楚,所以我想我会分享。这是我禁用两指滚动的方法:

// set up a two-finger pan recognizer as a dummy to steal two-finger scrolls from the scroll view
// we initialize without a target or action because we don't want the two-finger pan to be handled
UIPanGestureRecognizer *twoFingerPan = [[UIPanGestureRecognizer alloc] init];
twoFingerPan.minimumNumberOfTouches = 2;
twoFingerPan.maximumNumberOfTouches = 2;
[scrollView addGestureRecognizer:twoFingerPan];

I realize this is an old thread, but it took me a long time to figure this out, so I thought I would share. Here's what I did to disable two-finger scrolling:

// set up a two-finger pan recognizer as a dummy to steal two-finger scrolls from the scroll view
// we initialize without a target or action because we don't want the two-finger pan to be handled
UIPanGestureRecognizer *twoFingerPan = [[UIPanGestureRecognizer alloc] init];
twoFingerPan.minimumNumberOfTouches = 2;
twoFingerPan.maximumNumberOfTouches = 2;
[scrollView addGestureRecognizer:twoFingerPan];
孤云独去闲 2024-12-24 05:44:54

如果您使用iOS SDK 5.0以上,也许您可​​以直接使用ui pan手势识别器。

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

......

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

if you are using iOS SDK over 5.0, maybe you can use ui pan gesture recognizer directly.

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

......

[self.scrollView.panGestureRecognizer setMinimumNumberOfTouches:1];
[self.scrollView.panGestureRecognizer setMaximumNumberOfTouches:1];
静赏你的温柔 2024-12-24 05:44:54

我可以确认这在 iOS 8 中仍然是一个问题,但前提是 UIPanGestureRecognizer 位于 UIScrollView 的底层。使用新的 UIPanGestureRecognizer 创建 UIView 并设置其 MaximumNumberOfTouches 属性按预期工作。

提交了 rdar://20890684 并复制到 http://openradar.appspot.com/radar?id= 6191825677189120。请随意欺骗。

I can confirm this is still an issue in iOS 8, but only when the UIPanGestureRecognizer is underlying a UIScrollView. Creating a UIView with a fresh UIPanGestureRecognizer and setting its maximumNumberOfTouches property works as expected.

Submitted rdar://20890684 and copied to http://openradar.appspot.com/radar?id=6191825677189120. Please feel free to dupe.

惟欲睡 2024-12-24 05:44:54

问题

UIPanGestureRecognizer位于UIScrollView底层时> (不幸的是,这也会影响 UIPageViewControllermaximumNumberOfTouches 的行为不符合预期 - minimumNumberOfTouches 但是始终正确限制下端。

当监视这些参数时,它们似乎完成了它们的工作 - 只是 UIScrollView 不尊重它们并忽略它们的值!


补救措施

您可以在我的回答中找到解决方案:

UIScrollView 仅用一根手指滚动

PROBLEM:

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.

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


REMEDY:

You can find the solution in my answer to:

UIScrollView scrolling only with one finger

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