簡體   English   中英

集成 UIKit 和 Swift UI 時如何創建自定義 updateUIViewController 行為?

[英]How to create custom updateUIViewController behavior when integrating UIKit and Swift UI?

我有一個如下所述的視圖 controller。

//
//  PageViewController.swift


import SwiftUI
import UIKit

struct PageViewController: UIViewControllerRepresentable {
    var controllers: [UIViewController]
    @Binding var currentPage: Int
    init(controllers: [UIViewController], currentPage: Binding<Int>){
        self.controllers = controllers
        self._currentPage = currentPage
    }
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    func makeUIViewController(context: Context) -> UIPageViewController {
        let pageViewController = UIPageViewController(
            transitionStyle: .scroll,
            navigationOrientation: .vertical
        )
        pageViewController.dataSource = context.coordinator
        pageViewController.delegate = context.coordinator
        return pageViewController
    }

    func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) {
            pageViewController.setViewControllers(
            [controllers[currentPage]], direction: .forward, animated: true)
    }


    class Coordinator: NSObject, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
        var parent: PageViewController
        init(_ pageViewController: PageViewController) {
            self.parent = pageViewController
        }

        func pageViewController(
            _ pageViewController: UIPageViewController,
            viewControllerBefore viewController: UIViewController) -> UIViewController?
        {
            guard let index = parent.controllers.firstIndex(of: viewController) else {
                return nil
            }
            if index == 0 {
                return nil
            }

            return parent.controllers[index - 1]
        }

        func pageViewController(
            _ pageViewController: UIPageViewController,
            viewControllerAfter viewController: UIViewController) -> UIViewController?
        {
            guard let index = parent.controllers.firstIndex(of: viewController) else {
                return nil
            }
            if index + 1 == parent.controllers.count {
                return nil
            }
            return parent.controllers[index + 1]
        }
        func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
            if completed,
                let visibleViewController = pageViewController.viewControllers?.first,
                let index = parent.controllers.firstIndex(of: visibleViewController)
            {
                parent.currentPage = index
            }
        }
    }
}

在我的 swiftUI 文件中,我有類似的內容:

struct myCoolView : View { 
    @State var currentPage: Int = 0 
    var body: some View { 
        let subViews = [UIHostingController(rootView: mySubView(currentPage: $currentPage), ...]
        return VStack{ 
            PageViewController(controllers: subViews, currentPage: $currentPage)
        }

在我的子視圖中,我在堆棧中向后和向前有指向 go 的按鈕:

struct mySubView : View {
    @Binding var currentPage: Int

    var body: some View {
        HStack {
            Button(action: {self.currentPage -= 1}){
                Text("Go Back")
            }
            Spacer()
            Button(action: {self.currentPage += 1 }){
                Text("Go Forward")
            }
        }
    }
}

go 前進按鈕行為正確,協調器調用 updateUIViewController 激活.forward animation 樣式。 問題是后退按鈕在應該調用前進時也顯示前進 animation 樣式。

我玩弄了幾種方法來解決這個問題,但它們都沒有真正起作用。 我真正想要的是創建一個擴展來顯式觸發 go 向前或 go 向后更新流。 任何想法如何做到這一點?

提前致謝

對於完全相同的示例,我遇到了完全相同的問題。

我通過將可見 viewController 的索引與“currentIndex”進行比較並相應地調整方向來解決它。

func updateUIViewController(_ pageViewController: UIPageViewController, context: UIViewControllerRepresentableContext<PageViewController>) {
    
    var direction: UIPageViewController.NavigationDirection = .forward

    if let visibleViewController = pageViewController.viewControllers?.first,
        let index = controllers.firstIndex(of: visibleViewController),
        index > currentPage {
        direction = .reverse
    }

    pageViewController.setViewControllers([controllers[currentPage]], direction: direction, animated: true)
}

在我看來你有一個

 @State var currentPage: Int = 0

您用於許多 UIHostingController/mySubView 以及 PageViewController。 嘗試為每次使用使用不同的(一組)var。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM