Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:21:37+00:00 2026-06-13T16:21:37+00:00

I did the following code where three views can be reused during Pagination in

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:21:38+00:00Added an answer on June 13, 2026 at 4:21 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code (for simplicity, I did not follow any C# coding rules).
I tried the following code. Although I don't get any errors, it did not
Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
How can I create a const boost matrix? The following did not work: const
In the following code I have to reuse the Active Pattern result three times
I did the following steps to use the CDialog in win 32 application: Changed
I did the following but it not work for always.It works if i launch
Im a clojure beginner.I did the following steps as given on http://www.unexpected-vortices.com/clojure/brief-beginners-guide/development-env.html to setup
hay all, I just did the following: a = input(give a word: ) b
Did you ever have the following situation: you need to store information, but a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.