UIPageViewControllers UIPageControl在过渡期间出现故障

时间:2018-07-26 12:12:29

标签: swift uipageviewcontroller

我有三个控制器,这些控制器应通过UIPageViewController显示。它工作正常,但是UIPageControl在过渡期间不会闪烁。我在这里想念/看不到什么?

我将向您展示我的UIPageViewController实现,ViewController实现以及用于推送它的代码。谢谢您的时间!

我创建UIPageViewController及其包含的控制器的代码:

let blueVC = EmptyViewController()
blueVC.view.backgroundColor = UIColor.blue
blueVC.title = "blauer Controller"

let greenVC = EmptyViewController()
greenVC.view.backgroundColor = UIColor.green
greenVC.title = "grüner Controller"

let redVC = EmptyViewController()
redVC.view.backgroundColor = UIColor.red
redVC.title = "roter Controller"

let subViewControllers = [blueVC, greenVC, redVC]

let pageViewController = DemoPageViewController(withControllers: subViewControllers)

self.navigationController?.pushViewController(pageViewController, animated: true)

PageViewController:

import UIKit

class DemoPageViewController: UIPageViewController {
    private var myViewControllers = [UIViewController]()

    public init(withControllers controllers: [UIViewController]) {
        self.myViewControllers = controllers
        super.init(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        self.setViewControllers([controllers.first!], direction: .forward, animated: true, completion: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.backgroundColor = UIColor.clear
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        // Do any additional setup after loading the view.
    }
}

extension DemoPageViewController: UIPageViewControllerDataSource {
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = self.myViewControllers.index(of: viewController) else {
            return nil
        }

        let previousIndex = viewControllerIndex - 1

        guard previousIndex >= 0 else {
            return self.myViewControllers.last
        }

        guard self.myViewControllers.count > previousIndex else {
            return nil
        }

        return self.myViewControllers[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = self.myViewControllers.index(of: viewController) else {
            return nil
        }

        let nextIndex = viewControllerIndex + 1

        guard nextIndex < self.myViewControllers.count else {
            return self.myViewControllers.first
        }

        guard self.myViewControllers.count > nextIndex else {
            return nil
        }

        return self.myViewControllers[nextIndex]
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return self.myViewControllers.count
    }

    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
        return 0
    }
}

EmptyViewControllers的其他实现:

import Foundation
import PureLayout
import UIKit

class EmptyViewController: UIViewController {
    var didSetupConstraints = false

    override func loadView() {
        self.view = UIView()
        self.view.backgroundColor = UIColor.white

        self.view.setNeedsUpdateConstraints() // bootstrap Auto Layout
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("\(#function) in \(#file)")
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func updateViewConstraints() {
        // Check a flag didSetupConstraints before creating constraints, because this method may be called multiple times, and we
        // only want to create these constraints once. Without this check, the same constraints could be added multiple times,
        // which can hurt performance and cause other issues. See Demo 7 (Animation) for an example of code that runs every time.
        if !self.didSetupConstraints {
            // set Constraints here

            self.didSetupConstraints = true
        }

        super.updateViewConstraints()
    }
}

1 个答案:

答案 0 :(得分:0)

我刚刚找到原因。闪烁是由背景中的ViewController引起的,因为背景清晰,因此可以看到它。将背景设置为黑色确实有帮助。

相关问题