這是我第一次嘗試iOS應用程序,並且我沒有使用swift的經驗,並且很多代碼都是從Web中借用並編輯的。以編程方式推進UIPageViewController - Swift
我正在嘗試創建一組幻燈片。我從主着陸頁轉到另一個運行幻燈片的View Controller,TestVC。登陸頁面和幻燈片工作。我可以來回滑動。我現在正在嘗試添加計時器,以便幻燈片每5秒鐘左右自動前進一次。
我認爲,這需要運行的代碼是:
pageViewController.setViewControllers(varPageVC, direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
我得到一個錯誤:
test.swift:31:9: Ambiguous reference to member 'pageViewController(_:viewControllerBefore:)'.
我不知道如何解釋這個錯誤,繼續前進。該錯誤在test.swift
中觸發,其中計時器調用嘗試前進幻燈片的函數。建議表示讚賞。如果我做錯了,請指出我的方向。
登陸頁面有一個按鈕,打開一個ViewController
testVC。我有2個文件,test.swift
和alphabetItemController.swift
。故事板除了登陸頁面ViewController
之外,還有稱作字母PVC的,稱爲字母VC的ViewController
和稱爲TestVC的ViewController
。
這裏是alphabetItemController.swift代碼...
import UIKit
class alphabetItemController: UIViewController {
@IBOutlet weak var contentImageView2: UIImageView!
@IBOutlet weak var contentWordPn: UILabel!
var itemIndex: Int = 0
var imageName: String = ""
var wordPN: String = ""
var tTime: Timer!
override func viewDidLoad() {
super.viewDidLoad()
contentImageView2!.image = UIImage(named: imageName)
contentWordPn!.text = wordPN
}
}
這裏是test.swift代碼...
import Foundation
import UIKit
class testItemController: UIViewController, UIPageViewControllerDataSource {
var tTime: Timer!
override func viewDidLoad() {
super.viewDidLoad()
createPageViewController()
setupPageControl()
tTime = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(changeSlide), userInfo: nil, repeats: true)
//tTime = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(goToNextPage), userInfo: nil, repeats: true)
}
func changeSlide() {
pageViewController.setViewControllers(varPageVC, direction: UIPageViewControllerNavigationDirection.forward, animated: true, completion: nil)
}
// MARK: - Variables
private var varPageVC: UIPageViewController?
private let contentTextWordPN = ["A", "B", "C", "D", "E"]
private let contentCount = 5 //TODO ADJUST THIS FOR EACH COLLECTION
private func createPageViewController() {
let pageController = self.storyboard!.instantiateViewController(withIdentifier: "alphabetPVC") as! UIPageViewController
pageController.dataSource = self
if contentCount > 0 {
let firstController = getItemController(itemIndex: 0)!
let startingViewControllers = [firstController]
pageController.setViewControllers(startingViewControllers, direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
}
varPageVC = pageController
addChildViewController(varPageVC!)
self.view.addSubview(varPageVC!.view)
varPageVC!.didMove(toParentViewController: self)
}
private func setupPageControl() {
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.gray
appearance.currentPageIndicatorTintColor = UIColor.white
appearance.backgroundColor = UIColor.darkGray
}
func pageViewController(_ varPageVC: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! alphabetItemController
if itemController.itemIndex > 0 {
return getItemController(itemIndex: itemController.itemIndex-1)
}
return nil
}
func pageViewController(_ varPageVC: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let itemController = viewController as! alphabetItemController
if itemController.itemIndex+1 < contentCount {
return getItemController(itemIndex: itemController.itemIndex+1)
}
return nil
}
private func getItemController(itemIndex: Int) -> alphabetItemController? {
if itemIndex < contentCount {
let pageItemController = self.storyboard!.instantiateViewController(withIdentifier: "alphabetVC") as! alphabetItemController
pageItemController.itemIndex = itemIndex
pageItemController.imageName = "alphabet_" + String(format: "%02d", (itemIndex + 1)) //alphabet_01
pageItemController.wordPN = contentTextWordPN[itemIndex]
return pageItemController
}
return nil
}
func presentationCountForPageViewController(varPageVC: UIPageViewController) -> Int {
return contentCount
}
func presentationIndexForPageViewController(varPageVC: UIPageViewController) -> Int {
return 0
}
func currentControllerIndex() -> Int {
let pageItemController = self.currentController()
if let controller = pageItemController as? alphabetItemController {
return controller.itemIndex
}
return -1
}
func currentController() -> UIViewController? {
if (self.varPageVC?.viewControllers?.count)! > 0 {
return self.varPageVC?.viewControllers![0]
}
return nil
}
}
extension UIPageViewController {
func goToNextPage(animated: Bool = true) {
guard let currentViewController = self.viewControllers?.first else { return }
guard let nextViewController = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) else { return }
setViewControllers([nextViewController], direction: .forward, animated: animated, completion: nil)
}
func goToPreviousPage(animated: Bool = true) {
guard let currentViewController = self.viewControllers?.first else { return }
guard let previousViewController = dataSource?.pageViewController(self, viewControllerBefore: currentViewController) else { return }
setViewControllers([previousViewController], direction: .reverse, animated: animated, completion: nil)
}
}
甚至還有一個擴展UIPageViewController
,但我不知道如何調用goToNextPage函數。
如果您的幻燈片是圖片,您可能會發現在分頁模式下使用UICollectionView會更容易。不過,你正在UIPageViewController上創建一個擴展,所以你可以調用'pageViewController.goToNextPage'。 – Michael
此外,你從不在任何地方存儲'pageViewController',這可能是你爲什麼會得到錯誤。目前'createPageViewController'在局部變量中創建控制器,因此在proc結束時超出了範圍。 – Michael
謝謝!我最終改變了changeSlide()函數來調用varPageVC?.goToNextPage() –