UIScrollView: paging horizontally, scrolling vertically?

后端 未结 20 1458
佛祖请我去吃肉
佛祖请我去吃肉 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:41

    This is improved solution of icompot, which was quite unstable for me. This one works fine and it's easy to implement:

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

提交回复
热议问题