How do you combine UIScrollview with UIPagecontrol to show different views?

aggiesfan64 picture aggiesfan64 · Aug 20, 2010 · Viewed 23.4k times · Source

I've searched and searched for a tutorial for this but none of them are what I'm looking for. I've tried Apple's sample but it is just colors and I don't know how to make it views. All I'm looking for is a screen that will page while showing the page control. Each time the scroll view pages i want it to show a completely different view. Not different text or images but a different view. A lot like the home screen of the iPhone or ESPN Scorecenter app. Please Help! Thank you.

Answer

Renetik picture Renetik · Jan 9, 2013

I created this universal solution as examples found was to complicated and this is readable for me, code should be self explanatory.

- (IBAction)changePage:(id)sender {
    _pageControlUsed = YES;
    CGFloat pageWidth = _scrollView.contentSize.width /_pageControl.numberOfPages;
    CGFloat x = _pageControl.currentPage * pageWidth;
    [_scrollView scrollRectToVisible:CGRectMake(x, 0, pageWidth, _scrollView.frame.size.height) animated:YES];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    _pageControlUsed = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (!_pageControlUsed)
            _pageControl.currentPage = lround(_scrollView.contentOffset.x /
            (_scrollView.contentSize.width / _pageControl.numberOfPages));
}