How to implement UIScrollView with 1000+ subviews?

前端 未结 3 1630
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 19:30

I am struggling with writing portion of an app which should behave like the native iphone photo app. Looked at iphone sdk app development book from Orielly which gave an exa

3条回答
  •  忘掉有多难
    2020-12-07 20:00

    Many thanks to Adrian for his very simple and powerfull code sample. There was just one issue with this code : when the user made a "double scroll" (I.E. when he did not wait for the animation to stop and rescroll the scrollview again and again).

    In this case, the refresh for the position of the 3 subviews is effective only when the "scrollViewDidEndDecelerating" method is invoked, and the result is a delay before the apparition of the subviews on the screen.

    This can be easily avoided by adding few lines of code :

    in the interface, just add this :

    int refPage, currentPage;
    

    in the implementation, initialize refPage and currentPage in the "viewDidLoad" method like this :

    refpage = 0; 
    curentPage = 0;
    

    in the implementation, just add the "scrollViewDidScroll" method, like this :

    - (void)scrollViewDidScroll:(UIScrollView *)sender{
        int currentPosition = floor(_scrollView.contentOffset.x);
        currentPage = MAX(0,floor(currentPosition / 340)); 
    //340 is the width of the scrollview...
        if(currentPage != refPage)  { 
            refPage = currentPage;
            [self replaceHiddenLabels];
            }
        }
    

    et voilà !

    Now, the subviews are correctly replaced in the correct positions, even if the user never stop the animation and if the "scrollViewDidEndDecelerating" method is never invoked !

提交回复
热议问题