以编程方式推进UIPageViewController - Swift

时间:2017-09-01 03:00:03

标签: ios swift uipageviewcontroller

这是我第一次尝试使用IOS应用程序,而且我没有使用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.swiftalphabetItemController.swift。除了登录页面ViewController之外,故事板还有PageViewController名为alphabetPVC,ViewController名为alphabetVC,ViewController名为TestVC。

这是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函数。

1 个答案:

答案 0 :(得分:0)

我最后更改了changeSlide()函数...我已经在扩展中使用了下一个幻灯片函数,只有调用它的语法让我不知所措......我在SO上找到了一些示例并使用了它们作为参考:

func changeSlide() {
    varPageVC?.goToNextPage()
}