OGeek|极客世界-中国程序员成长平台

标题: ios - 以编程方式推进 UIPageViewController - Swift [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:35
标题: ios - 以编程方式推进 UIPageViewController - Swift

这是我第一次尝试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 之外, Storyboard还有一个称为 alphabetPVC 的 PageViewController、一个称为 alphabetVC 的 ViewController 和一个 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函数。



Best Answer-推荐答案


我最终更改了 changeSlide() 函数...我已经在扩展中拥有下一个幻灯片功能,只有调用它的语法让我无法理解...我找到了一些示例在 SO 上并将它们用作引用:

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

关于ios - 以编程方式推进 UIPageViewController - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45992734/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4