UIPageViewControllers UIPageControl 在过渡期间出现故障

Posted

技术标签:

【中文标题】UIPageViewControllers UIPageControl 在过渡期间出现故障【英文标题】:UIPageViewControllers UIPageControl glitches during transition 【发布时间】:2018-07-26 12:12:29 【问题描述】:

我有三个将通过 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】:

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

【讨论】:

以上是关于UIPageViewControllers UIPageControl 在过渡期间出现故障的主要内容,如果未能解决你的问题,请参考以下文章

UIPageViewControllers UIPageControl 在过渡期间出现故障

STM32+ENC28J60+UIP协议栈实现WEB服务器示例

STM32+ENC28J60+UIP协议栈实现WEB服务器示例

uip UDP server广播模式(client能够随意port,而且主动向client发送数据)

ALIENTEK 战舰ENC28J60 LWIP和UIP补充例程(LWIP WEB有惊喜)

布尔约束传播BCP——文献学习CDCL Solver Additions: Local Look-Ahead,All-Unit-UIP Learning and On-the-Fly Probing(