Check swift version of this tutorial: Learn how to create UIPageControl in swift
So let us dive in to UIPageControl Tutorial.
Create a singleView application, and name it UIPageControlDemo. After creating the project Xcode will display you below shown screen
|Main Interface after creating project in Xcode|
|Drag UIPageControl and UIScrollView to view|
Open Main.storyboard and connect IBOutlets and IBAction to UIScrollView and UIPageControl
|Connecting UIScrollView With IBOutlet|
|Connecting UIPageControl valueChanged event With IBAction|
|Setting IBAction to UIPageControl ValueChanged Event|
UIPageControl has properties to set minimum number of pages you want in your UIPageControl and which page do you want to set as current page. We will stick with default values for this tutorial.
|Set minimum pages and current page for UIPageControl|
Inside viewDidLoad method, we will add UIImageView to our scroll and assign its delegate to self.
We will set contentSize of scroll to number of pages we want in UIPageControl multiplied by width of UIScrollView.
Then, we add three UIImageView to our scroll and set different background color to it.
In our changePage action, we will use scrollRectToVisible method of UIScrollView so that we can scroll our UIScrollView as per user tap on UIPageControl.
Lastly. we ned to implement scrollViewDidEndDecelerating delegate of UIScrollView so that we can calculate page number if user did not use UIPageControl and simple scroll down UIScrollView.
In setIndicatorForCurrentPage, we will calculate page by diving x offset of UIScrollView with its width and the assign that page to our pageControl as its current or selected page.
Source Code: To Download Click