How to make infinite scroll view in iPhone?

前端 未结 7 1018
无人共我
无人共我 2021-01-01 00:50

I have a scrollview with 5 image views of width 88.

I want the scrollview to scroll to each image view (Not Pagin

7条回答
  •  独厮守ぢ
    2021-01-01 01:27

    First of all, I recommend to use a UITableView so you can maintain the memory usage low. An approach that I had used successfully in a project is the following:

      1. List item

    Duplicate the content of your cells, I mean if you have 5 items in this way:

    | 1 | 2 | 3 | 4 | 5 |

    You should add the same (In a table view with reusable engine that's not a big deal) at the end of the table in order to look like this:

    | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 |

      2. modify the scrollViewDidScroll with the following:
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if (scrollView == _yourScrollView) {
            CGFloat currentOffsetX = scrollView.contentOffset.x;
            CGFloat currentOffSetY = scrollView.contentOffset.y;
            CGFloat contentHeight = scrollView.contentSize.height;
    
            if (currentOffSetY < (contentHeight / 6.0f)) {
                scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY + (contentHeight/2)));
            }
            if (currentOffSetY > ((contentHeight * 4)/ 6.0f)) {
                scrollView.contentOffset = CGPointMake(currentOffsetX,(currentOffSetY - (contentHeight/2)));
            }
        }
    }
    

    The code above move the scroll position at top if you almost reach the final of the scrolling; Or if you are almost on the top, moves you to the bottom...

      3. That's it.

提交回复
热议问题