xcode UIScrollView 水平滚动 - 禁用左滚动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5041160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:15:24  来源:igfitidea点击:

UIScrollView Horizontal scrolling - Disable left scroll

iphoneobjective-cxcode

提问by user609906

How can I disable scrolling left on a UIScrollview. This is so users can only scroll right?

如何在 UIScrollview 上禁用向左滚动。这样用户就只能滚动了吧?

Thanks

谢谢

***UPDATE *****

** *更新*****

This is what I have now. I have subclassed UIScrollView and overwritten the following methods

这就是我现在所拥有的。我已经继承了 UIScrollView 并覆盖了以下方法

@implementation TouchScrollView

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
    NSLog(@"touchesShouldBegin: I was touched!");
    return YES;
}

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    NSLog(@"touchesShouldCancelInContentView: I was touched!");
    return NO;
}

In my viewController I have also set the following attributes:

在我的 viewController 中,我还设置了以下属性:

scrollView.delaysContentTouches = NO;
scrollView.canCancelContentTouches = YES;

TouchesShouldBegin is being called but touchesShouldCancelInContentView is not being called and I cant figure out why!!

正在调用 TouchesShouldBegin 但没有调用 touchesShouldCancelInContentView,我不知道为什么!!

Thanks

谢谢

回答by meronix

just add this in your UIViewController UIScrollViewDelegate

只需在您的 UIViewController UIScrollViewDelegate 中添加它

float oldX; // here or better in .h interface

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
    if (scrollView.contentOffset.x < oldX) {
        [aScrollView setContentOffset: CGPointMake(oldX, aScrollView.contentOffset.y)];
    } else {
        oldX = aScrollView.contentOffset.x;
    }

}

回答by knuku

You should handle swipe gestures.

您应该处理滑动手势。

Check similar question here: iOS Advanced Gestures: Getting Swipe Direction Vector

在此处检查类似问题:iOS 高级手势:获取滑动方向矢量