Reusing 3 views on UIScrollView's Paging

后端 未结 3 1205
被撕碎了的回忆
被撕碎了的回忆 2021-02-11 04:02

I did the following code where three views can be reused during Pagination in UIScrollView in order to save live memory-->

    #pragma mark - UIScro         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-02-11 04:18

    Its ok, but you have too much code (~40 lines) and too much unnecessary processing. You only need to know when one frame matches (let's say the center frame), and also you should do this only when the page is about to change, not on every scroll event.

    This way, whenever the left or right page becomes the current page, you move the opposite page to the other side.

    Another bug you had is that you should hide the last+1 page when its frame.origin.x is equal (== , or >=) to the content size, not only bigger (>).

    #pragma mark - UIScrollView Delegates
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
        int selectedPage = roundf(newsPagesView.contentOffset.x/_pageWidth);
    
        if (selectedPage != _currentPage) {
    
            _currentPage = selectedPage;        
            [self update:selectedPage];
        }
    
    
    }
    
    
    #pragma mark - Custom methods
    -(void)update:(int) selectedPage{
    
        BOOL page1FrameMatched = false;
        BOOL page2FrameMatched = false;
        BOOL page3FrameMatched = false;
    
        BOOL frameCurrentMatched = false;
    
    
        CGRect frameCurrent = CGRectMake(selectedPage*_pageWidth, 0.0f, _pageWidth, _pageHeight);
        CGRect frameLeft = CGRectMake((selectedPage-1)*_pageWidth, 0.0f, _pageWidth, _pageHeight);
        CGRect frameRight = CGRectMake((selectedPage+1)*_pageWidth, 0.0f, _pageWidth, _pageHeight);
    
        NewsPage *page1 = (NewsPage*)[newsPagesView viewWithTag:100];
        NewsPage *page2 = (NewsPage*)[newsPagesView viewWithTag:101];
        NewsPage *page3 = (NewsPage*)[newsPagesView viewWithTag:102];
    
        if(page1 && page2 && page3){
    
            //Check for Current
            if(frameCurrent.origin.x == page1.frame.origin.x){
                page1FrameMatched = true;
                frameCurrentMatched = true;
            }
            else if(frameCurrent.origin.x == page2.frame.origin.x){
                page2FrameMatched = true;
                frameCurrentMatched = true;
            }
            else if(frameCurrent.origin.x ==page3.frame.origin.x){
                page3FrameMatched = true;
                frameCurrentMatched = true;
            }
    
            if(frameCurrentMatched){
                if(page1FrameMatched){
                    [page1 setFrame:frameCurrent];
                    [page2 setFrame:frameLeft];
                    [page3 setFrame:frameRight];
    
                }
                else if(page2FrameMatched){
                    [page1 setFrame:frameRight];
                    [page2 setFrame:frameCurrent];
                    [page3 setFrame:frameLeft];
                }
                else{
                    [page1 setFrame:frameLeft];
                    [page2 setFrame:frameRight];
                    [page3 setFrame:frameCurrent];
    
                }
    
                [self hideShowView:page1];
                [self hideShowView:page2];
                [self hideShowView:page3];
            }
    
        }
    }
    
    /**
     * This method hides the view if it is outside the scrollview content bounds, i.e. the
     * view before page 0, or the view after last page.
     */
    -(void)hideShowView:(NewsPage*)aPage{
    
        if(aPage.frame.origin.x<0 || aPage.frame.origin.x>=[newsPagesView contentSize].width )
            aPage.hidden = YES; 
        else{
            aPage.hidden = NO;
        }
    }
    

提交回复
热议问题