gpt4 book ai didi

swift - UIPageViewControllers UIPageControl 在转换过程中出现故障

转载 作者:行者123 更新时间:2023-11-30 11:18:33 25 4
gpt4 key购买 nike

我有三个 Controller ,将通过 UIPageViewController 显示。它工作得很好,但 UIPageControl 在转换过程中会闪烁。我在这里错过/没有看到什么?

我将向您展示我的 UIPageViewController 实现、ViewController 实现以及我用来推送它的代码。感谢您的宝贵时间!

我创建 UIPageViewController 及其包含的 Controller 的代码:

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()
}
}

最佳答案

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

关于swift - UIPageViewControllers UIPageControl 在转换过程中出现故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51538581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com