UIScrollView: paging horizontally, scrolling vertically?

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

    I might be gravedigging here, but I stumbled onto this post today as I was trying to solve the same problem. Here's my solution, seems to be working great.

    I want to display a screenful of content, but allow the user to scroll to one of 4 other screenfuls, up down left and right. I have a single UIScrollView with size 320x480 and a contentSize of 960x1440. The content offset starts at 320,480. In scrollViewDidEndDecelerating:, I redraw the 5 views (center views and the 4 around it), and reset the content offset to 320,480.

    Here's the meat of my solution, the scrollViewDidScroll: method.

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {   
        if (!scrollingVertically && ! scrollingHorizontally)
        {
            int xDiff = abs(scrollView.contentOffset.x - 320);
            int yDiff = abs(scrollView.contentOffset.y - 480);
    
            if (xDiff > yDiff)
            {
                scrollingHorizontally = YES;
            }
            else if (xDiff < yDiff)
            {
                scrollingVertically = YES;
            }
        }
    
        if (scrollingHorizontally)
        {
            scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 480);
        }
        else if (scrollingVertically)
        {
            scrollView.contentOffset = CGPointMake(320, scrollView.contentOffset.y);
        }
    }
    

提交回复
热议问题