在UIScrollView的分页上重用3个视图 [英] Reusing 3 views on UIScrollView's Paging

查看:86
本文介绍了在UIScrollView的分页上重用3个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了以下代码,其中三个视图可以在 UIScrollView 中的分页期间重复使用以节省实时内存 - >

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

    #pragma mark - UIScrollView Delegates
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGFloat pageWidth = self.view.frame.size.width;
    CGPoint aContentOffSet = [[self scrollView] contentOffset] ;
    float currPos = aContentOffSet.x;
    int selectedPage = roundf(currPos/pageWidth);
    [[self pageControl] setCurrentPage:selectedPage];
    [self update:selectedPage];
}


#pragma mark - Custom methods
-(void)update:(int) selectedPage{

    BOOL view1FrameShallBeUnchanged = false;
    BOOL view2FrameShallBeUnchanged = false;
    BOOL view3FrameShallBeUnchanged = false;

    BOOL aFrame1Matched = false;
    BOOL aFrame2Matched = false;
    BOOL aFrame3Matched = false;

    CGRect aFrame1 = CGRectMake(selectedPage*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);
    CGRect aFrame2 = CGRectMake((selectedPage-1)*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);
    CGRect aFrame3 = CGRectMake((selectedPage+1)*self.view.frame.size.width, 0.0f, self.view.frame.size.width, self.scrollView.frame.size.height);

    ViewOnScrollView *aView1 = (ViewOnScrollView*)[[self scrollView] viewWithTag:1234];
    ViewOnScrollView *aView2 = (ViewOnScrollView*)[[self scrollView] viewWithTag:12345];
    ViewOnScrollView *aView3 = (ViewOnScrollView*)[[self scrollView] viewWithTag:123456];

    if(aView1 && aView2 && aView3){
    //Check for Frame 1
    if(aFrame1.origin.x == aView1.frame.origin.x){
        view1FrameShallBeUnchanged = true;
        aFrame1Matched = true;
    }
    else if(aFrame1.origin.x == aView2.frame.origin.x){
        view2FrameShallBeUnchanged = true;
        aFrame1Matched = true;
    }
    else if(aFrame1.origin.x ==aView3.frame.origin.x){
        view3FrameShallBeUnchanged = true;
        aFrame1Matched = true;
    }

    //Check for Frame 2
    if(aFrame2.origin.x == aView1.frame.origin.x){
        view1FrameShallBeUnchanged = true; 
        aFrame2Matched = true;
    }
    else if(aFrame2.origin.x == aView2.frame.origin.x){
        view2FrameShallBeUnchanged = true;
        aFrame2Matched = true;
    }
    else if(aFrame2.origin.x == aView3.frame.origin.x){
        view3FrameShallBeUnchanged = true;
        aFrame2Matched = true;
    }

    //Check for Frame 3
    if(aFrame3.origin.x == aView1.frame.origin.x){
        view1FrameShallBeUnchanged = true;
        aFrame3Matched = true;
    }
    else if(aFrame3.origin.x == aView2.frame.origin.x){
        view2FrameShallBeUnchanged = true;
        aFrame3Matched = true;
    }
    else if(aFrame3.origin.x == aView3.frame.origin.x){
        view3FrameShallBeUnchanged = true;
        aFrame3Matched = true;
    }


    if(!view1FrameShallBeUnchanged){
        if(!aFrame1Matched){
            [aView1 setFrame:aFrame1];
        }
        else if(!aFrame2Matched){
            [aView1 setFrame:aFrame2];
        }
        else{
            [aView1 setFrame:aFrame3];
        }
        [self hideOrShowTheTabs:aView1];
        [self hideShowView:aView1];
    }

    if(!view2FrameShallBeUnchanged){
        if(!aFrame1Matched){
            [aView2 setFrame:aFrame1];
        }
        else if(!aFrame2Matched){
            [aView2 setFrame:aFrame2];
        }
        else{
            [aView2 setFrame:aFrame3];
        }

        [self hideShowView:aView2];
    }

    if(!view3FrameShallBeUnchanged){
        if(!aFrame1Matched){
            [aView3 setFrame:aFrame1];
        }
        else if(!aFrame2Matched){
            [aView3 setFrame:aFrame2];
        }
        else{
            [aView3 setFrame:aFrame3];           
        }

        [self hideShowView:aView3];
    }
    }
}

-(void)hideShowView:(ViewOnScrollView*)theView{
    if(theView.frame.origin.x<0 || theView.frame.origin.x>[self.scrollView contentSize].width )
        theView.hidden = YES; 
    else{
        theView.hidden = NO;
    }
}

评论/建议/更好的方法是做同样的事情欢迎..

Comments/Suggestions/Better ways to do the same are welcome..

推荐答案

没关系,但你有太多代码(约40行)和太多不必要的处理。您只需知道一帧何时匹配(假设中心帧),并且您应该仅在页面即将更改时执行此操作,而不是每次滚动事件。

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.

你遇到的另一个错误是你应该当 frame.origin.x 与内容大小相等(==或> =)时,隐藏最后的+1页,不仅更大(>)。

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;
    }
}

这篇关于在UIScrollView的分页上重用3个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆