UIScrollView: paging horizontally, scrolling vertically?

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

    I used following method to solve this problem, hope it helps you.

    First define following variables in your controllers header file.

    CGPoint startPos;
    int     scrollDirection;
    

    startPos will keep the contentOffset value when your delegate receives scrollViewWillBeginDragging message. So in this method we do this;

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
        startPos = scrollView.contentOffset;
        scrollDirection=0;
    }
    

    then we use these values to determine users intended scroll direction in scrollViewDidScroll message.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
       if (scrollDirection==0){//we need to determine direction
           //use the difference between positions to determine the direction.
           if (abs(startPos.x-scrollView.contentOffset.x)

    finally we have to stop all update operations when user end dragging;

     - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
        if (decelerate) {
           scrollDirection=3;
        }
     }
    

提交回复
热议问题