UIScrollView disable scrolling in just one direction?

前端 未结 10 1810
暗喜
暗喜 2020-12-13 09:40

I\'ve been unable to find an answer for this (maybe someone has hacked a solution together).

Is it possible to disable scrolling in a UIScrollView in one direction?

相关标签:
10条回答
  • 2020-12-13 10:12

    It's possible to remove scrolling in vertical direction by setting the scrollView.contentSize height to the same value as scrollView.frame.size.height. Any overflowing content will be hidden. Same can of course be done to restrict vertical scrolling.

    0 讨论(0)
  • 2020-12-13 10:14

    this works for me

    static CGPoint lastOffset;
    
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
        scrollView.scrollEnabled = YES;
    }
    
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
        scrollView.scrollEnabled = YES;
        lastOffset = scrollView.contentOffset;
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        CGPoint nowOffset = scrollView.contentOffset;
        NSLog(@"delta %f", lastOffset.x - nowOffset.x);
        if ((lastOffset.x - nowOffset.x) < 0) {
            //uncomment to prevent scroll to left
            //scrollView.scrollEnabled = NO;
        } else if ((lastOffset.x - nowOffset.x) > 0) {
            //uncomment to prevent scroll to right
            //scrollView.scrollEnabled = NO;
        } else {
            scrollView.scrollEnabled = YES;
        }
    }
    
    0 讨论(0)
  • 2020-12-13 10:14

    An alternative might be:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        if (scrollView.contentOffset.y < 10) {
            scrollView.bounces = NO;
        }
    
        else scrollView.bounces = YES;
    }
    

    But remember that this will only work for the scrollViews, which have a bigger content than their frames.

    0 讨论(0)
  • 2020-12-13 10:20

    Instead of using a UIScrollViewDelegate to correct the already wrong contentOffset (which will result in a jittery behaviour) you may want to consider to sub class UIScrollView instead and overriding setContentOffset:

    - (void)setContentOffset:(CGPoint)contentOffset {
        if (contentOffset.y <= 60) {
            [super setContentOffset:contentOffset];
        }
    }
    

    Off course this can be generalized by adding a property for the min or max allowed value for the content offset. You may need to override setContentOffset:animated: as well.

    0 讨论(0)
  • 2020-12-13 10:26

    If you set the contentSize of the scroller equals to the size of the content, in one of the direction, the scroll will disappear in that direction because there will be nothing to scroll.

    0 讨论(0)
  • 2020-12-13 10:29

    Turns out a simple solution was actually possible and easy:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        if (scrollView.contentOffset.y > 60) {
            [scrollView setContentOffset:CGPointMake(0, 60)];
        }
    }
    
    0 讨论(0)
提交回复
热议问题