效果图:
----------FirstPageViewController-----------
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.brownColor()
}
----------SecondPageViewController-----------
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.purpleColor()
}
----------ThirdPageViewController-----------
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.blueColor()
}
---------------PageViewController--------------------
//
// PageControlViewController.swift
//
import UIKit
class PageControlViewController: UIViewController, UIScrollViewDelegate
{
var pageControl: UIPageControl?
var scrollView: UIScrollView?
var isPageControlUsed = false
var screenFrame :CGRect?
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "UIPageControl"
self.view.backgroundColor = UIColor.whiteColor()
screenFrame = UIScreen.mainScreen().bounds
let screenWidth = screenFrame!.width
let screenHeight = screenFrame!.height
var view = UIView(frame: CGRectMake(0, 0, screenWidth, 60))
view.backgroundColor = UIColor.whiteColor()
self.view.addSubview(view)
scrollView = UIScrollView(frame: CGRectMake(0, 60, screenWidth, screenHeight - 60))
pageControl = UIPageControl(frame: CGRectMake(0, screenHeight - 40, screenWidth, 40))
scrollView!.pagingEnabled = true
scrollView!.contentSize = CGSizeMake(screenWidth * 3, screenHeight - 60)
let firstViewController = FirstPageViewController()
firstViewController.view.frame = CGRectMake(0, 0, screenWidth, screenHeight - 60)
let secondViewController = SecondPageViewController()
secondViewController.view.frame = CGRectMake(screenWidth, 0, screenWidth, screenHeight - 60)
let thirdViewController = ThirdPageViewController()
thirdViewController.view.frame = CGRectMake(screenWidth * 2, 0, screenWidth, screenHeight - 60)
scrollView!.addSubview(firstViewController.view)
scrollView!.addSubview(secondViewController.view)
scrollView!.addSubview(thirdViewController.view)
scrollView!.delegate = self
self.view.addSubview(scrollView!)
pageControl!.numberOfPages = 3
pageControl!.currentPage = 0
pageControl!.addTarget(self, action: "pageDidChanged:", forControlEvents: UIControlEvents.ValueChanged)
self.view.addSubview(pageControl!)
installCodeBarBtn()
}
func pageDidChanged(obj : UIPageControl) -> Void
{
var currentPage = obj.currentPage
var frame = scrollView!.frame
frame.origin.x = (CGFloat)(currentPage) * frame.size.width
scrollView!.scrollRectToVisible(frame, animated: true)
isPageControlUsed = true
}
func scrollViewDidScroll(scrollView: UIScrollView)
{
if(!isPageControlUsed)
{
let pageWidth = scrollView.frame.size.width
let page = floor((scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1
pageControl!.currentPage = (Int)(page)
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView)
{
isPageControlUsed = false
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func installCodeBarBtn() -> Void
{
var rightBar = UIBarButtonItem(title: "Code", style: UIBarButtonItemStyle.Done, target: self, action: "toCodeViewPad")
self.navigationItem.rightBarButtonItem = rightBar
}
func toCodeViewPad() -> Void
{
let codeViewController = PageCodeViewController()
self.navigationController!.pushViewController(codeViewController, animated: true)
}
}