9

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.

aggiesfan64
  • 101
  • 1
  • 1
  • 3

5 Answers5

16

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));
}
Renetik
  • 5,887
  • 1
  • 47
  • 66
  • actually, look down at @datinc's answer. very clean and simple. – eric Mar 18 '14 at 22:13
  • @Rene Dohan how to get same effect on click changePage and scrollViewDidEndDecelerating. zip zip movement of images on scroll same has click on page control. – kiran Oct 24 '14 at 09:50
  • @ReneDohan absolutely! I do love that about SO! I only pointed the other solution out because yours had so many up-votes, and IMHO the newer answer was better. :-) – eric Dec 17 '14 at 01:24
  • I still use this solution because I like that DidEndDecelerating is called just once on and of event instead of multiple times in scrollViewDidScroll... This can have some other unwanted consequences... – Renetik Apr 19 '15 at 08:22
14

This does the same as @ReneDohan answer without the need for variable to store state

- (IBAction)changePage:(id)sender {
    CGFloat x = self.pageControl.currentPage * self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.isDragging || scrollView.isDecelerating){
        self.pageControl.currentPage = lround(self.scrollView.contentOffset.x / (self.scrollView.contentSize.width / self.pageControl.numberOfPages));
    }
}
datinc
  • 3,404
  • 3
  • 24
  • 33
4

Try out this framework : https://github.com/AdrianFlorian/AFImageViewer to present images in a scroll view using a page controll to indicate the current page.

I have't added documentation yet, but you can see examples if you clone the project.

You can easily: - download images from the internet in a separate thread by only giving an array of urls (image urls) - give it an array of UIImage objects - implement a delegate and manage the image for each page yourself

1

I've made a demo project for this question. Please visit: https://github.com/lenhhoxung86/PageControlDemo

lenhhoxung
  • 2,530
  • 2
  • 30
  • 61
1

There is a related question: How do I use UIPageControl to create multiple views?, a really good way to do that is explained in this blog post: http://cocoawithlove.com/2009/01/multiple-virtual-pages-in-uiscrollview.html.

Community
  • 1
  • 1
Ricardo Valeriano
  • 7,533
  • 1
  • 24
  • 34