UIScrollView: paging horizontally, scrolling vertically?

后端 未结 20 1403
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 09:09

How can I force a UIScrollView in which paging and scrolling are on to only move vertically or horizontally at a given moment?

My understanding is that

20条回答
  •  清酒与你
    2020-11-27 09:38

    For the lazy people like me:

    Set scrollview.directionalLockEnabled to YES

    - (void) scrollViewWillBeginDragging: (UIScrollView *) scrollView
    {
        self.oldContentOffset = scrollView.contentOffset;
    }
    
    - (void) scrollViewDidScroll: (UIScrollView *) scrollView
    {
        if (scrollView.contentOffset.x != self.oldContentOffset.x)
        {
            scrollView.pagingEnabled = YES;
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x,
                                                   self.oldContentOffset.y);
        }
        else
        {
            scrollView.pagingEnabled = NO;
        }
    }
    
    - (void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView
    {
        self.oldContentOffset = scrollView.contentOffset;
    }
    

提交回复
热议问题