I've been looking into using the UIPageControl for a scrolling part of an application, and I was wondering how it works. The docs show methods for changing pages, setting the number of pages etc., but by itself the page control doesn't encompass any kind of scroll view.
Are the UIPageControl and UIScrollView classes supposed to be used in conjunction when one wants to show the page indicator (like on the home screen of the iPhone with the app pages)?
UIPageControl does two things: (1) it displays dots, including a highlighted dot for the currently selected page, and (2) it generates a UIControlEventValueChanged event when the user taps on it. Tap on the right side of the control to page to the right; tap on the left side to page to the left. You should implement this behavior to be consistent with the HIG!
To trap the event, add:
and that delegate function as well:
The name can be whatever you want; I used "pageChanged" for example. The callback signature can be pageChanged, pageChanged:, or pageChanged:forEvent:. This callback function should cause your scrollview (or what have you) to update.
Paging, itself, is usually done by using a UIScrollView with paging enabled and scrollbars hidden. Tell the scrollview to use paging and flicks magically move page by page; make sure you trap that change to update the currentPage property of the UIPageControl.
I've written a class (PagedView) which works just like UITableView and merges the concepts of the UIPageControl and UIScrollView in a manner used for example on the iPhone home screen.
The concept is basically as follows: you need to implement PagedViewDelegate to return the number of pages and a view for each page of you PagedView. Reusing of views works the same as in UITableView. Use interface builder to connect the scrollview and pageControl outlets.
Please let me know if you find this class useful.
.h-file:
.m-file:
You're correct--UIPageControl doesn't draw anything by itself other than the dots at the bottom of the page; you need another view to do that.
To use Werner Altewischer class add PagedViewDelegate to interface and complete your class as shown below:
The paging bit is actually done by setting the paging property on the UIScrollView. The page control is simply a useful UI construct, but doesn't actually do anything to do with paging itself.