我正在尝试在我的 swift 应用程序中实现类似书籍的界面,其中书脊位于中心,用户可以查看两页。为此,我使用 UIPageViewController 类并创建要在屏幕上显示的 ViewController 数组。
我能够正确初始化前两页。然而,当我翻页查看第 3 页和第 4 页时,它直接转到最后一页。我使用 ViewContorllerRespresentable 将其嵌入到我的 Swift UI 中,以便在屏幕上看到它。
这是我正在使用的代码。
import SwiftUI
import UIKit
import Combine
class CustomPageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let index = viewControllersList.firstIndex(of: viewController), index >= 2 else {
return nil
}
print("currrent index is", index)
let previousIndex = index - 2
print("MOVING BEHIND")
print(previousIndex+1)
print(previousIndex)
setViewControllers([viewControllersList[previousIndex], viewControllersList[previousIndex + 1]], direction: .reverse, animated: true){finished in
if finished{
print("completed move behind operation")
}
}
return nil // The system manages the transition; no need to return a single view controller.
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let index = viewControllersList.firstIndex(of: viewController), index < viewControllersList.count - 2 else {
return nil
}
print("currrent index is", index)
let nextIndex = index + 2
print("MOVING AHEAD")
print(nextIndex-1)
print(nextIndex)
setViewControllers([viewControllersList[nextIndex-1], viewControllersList[nextIndex]], direction: .forward, animated: true){finished in
if finished{
print("completed move ahead operation")
}
}
return nil // The system manages the transition; no need to return a single view controller.
}
lazy var viewControllersList: [UIViewController] = []
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
let firstViewController = UIViewController()
firstViewController.view.backgroundColor = .red
let secondViewController = UIViewController()
secondViewController.view.backgroundColor = .green
let thirdViewController = UIViewController()
thirdViewController.view.backgroundColor = .blue
let fourthViewController = UIViewController()
thirdViewController.view.backgroundColor = .yellow
let fifthViewController = UIViewController()
thirdViewController.view.backgroundColor = .black
let sixthViewController = UIViewController()
thirdViewController.view.backgroundColor = .magenta
viewControllersList = [firstViewController, secondViewController, thirdViewController, fourthViewController, fifthViewController, sixthViewController]
setViewControllers([viewControllersList[0], viewControllersList[1]], direction: .forward, animated: true){finished in
if finished{
print("set initial VC")
}
}
}
func pageViewController(_ pageViewController: UIPageViewController, spineLocationFor interfaceOrientation: UIInterfaceOrientation) -> UIPageViewController.SpineLocation {
if interfaceOrientation.isLandscape{
// self.isDoubleSided = true
print("INSIDE THIS BLOCK OF CODE")
if let currentViewController = self.viewControllers?.first, let currentIndex = viewControllersList.firstIndex(of: currentViewController) {
let nextIndex = currentIndex + 1
let nextViewController = (nextIndex < viewControllersList.count) ? viewControllersList[nextIndex] : nil
if let nextVC = nextViewController {
setViewControllers([currentViewController, nextVC], direction: .forward, animated: true, completion: nil)
} else {
setViewControllers([currentViewController], direction: .forward, animated: true, completion: nil)
}
}
return .mid
}
else{
return .min
}
}
}
// 2. PageViewControllerRepresentable
struct MyViewControllerRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> CustomPageViewController {
let pageViewController = CustomPageViewController(transitionStyle: .pageCurl, navigationOrientation: .horizontal)
return pageViewController
}
func updateUIViewController(_ uiViewController: CustomPageViewController, context: Context) {
// Update the UIPageViewController if needed
}
}
struct TestView : View {
var body: some View{
VStack{
MyViewControllerRepresentable()
}
}
}
我觉得我真的很接近实现它,但我错过了一个小细节。感谢您对此的帮助
对于函数 setViewControllers([viewControllersList[index]], ...),仅使用数组中的 1 viewcontroller 。这已经有帮助了