類PageViewController也是一個特殊的UIViewController,它可以管理多個頁面,每個頁面都是一個UIViewController,可程序控制或者用戶手勢來切換頁面。
如下代碼,展示了PageViewController的使用:
import UIKit
class VCBase: UIViewController
{
var label : UILabel?
override func viewDidLoad()
{
super.viewDidLoad()
label = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 200))
label!.textAlignment = .center
view.addSubview(label!)
view.backgroundColor = UIColor.white
}
}
class Page1: VCBase
{
override func viewDidLoad()
{
super.viewDidLoad()
label!.text = "#1"
}
}
class Page2: VCBase
{
override func viewDidLoad()
{
super.viewDidLoad()
label!.text = "#2"
}
}
class PageViewController :UIPageViewController,UIPageViewControllerDataSource{
var vcs :[UIViewController]
required init(){
vcs = [Page1(),Page2()]
super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
}
required init?(coder: NSCoder){
fatalError()
}
override func viewDidLoad() {
self.dataSource = self
setViewControllers( [vcs[0]], direction: .forward, animated: false, completion: nil)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
{
let v = (viewController)
let i = vcs.index(of: v)! - 1
if i < 0 {
return nil
}
return vcs[i]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
let v = (viewController)
let i = vcs.index(of: v)! + 1
if i > vcs.count - 1 {
return nil
}
return vcs[i]
}
func presentationCount(for pageViewController: UIPageViewController) -> Int
{
return vcs.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int
{
return 0
}
}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
var Window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.Window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = PageViewController()
self.window?.makeKeyAndVisible()
return true
}
}
前面的兩個類都是UIViewController的子類,每個初始化一個標簽,內容不同以示區別。類PageViewController是UIPageViewController的子類,實現了UIPageViewControllerDataSource並在裝入時設置數據源為自身。通過:
setViewControllers( [vcs[0]], direction: .forward, animated: false, completion: nil)
設置UIPageViewController的當前的ViewController。這裡設置的是Page1的實例,因此App啟動後顯示的初始頁面就是Page1。
協議UIPageViewControllerDataSource的內容如下:
public protocol UIPageViewControllerDataSource : NSObjectProtocol {
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
optional public func presentationCount(for pageViewController: UIPageViewController) -> Int // The number of items reflected in the page indicator.
optional public func presentationIndex(for pageViewController: UIPageViewController) -> Int // The selected item reflected in the page indicator.
}
其中前兩個函數會有UIKit傳入當前視圖,希望App返回此視圖的前一個視圖或者是後一個視圖。後兩個函數分別返回總頁面數量和初始頁面對應的選擇項索引,這兩個函數是可選實現的,如果實現了,就會在頁面底部顯示頁面指示器。
以上就是對PageViewController的相關介紹,希望對您學習IOS有所幫助,感謝您關注本站!
【PageViewController】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!