如何在集成 UIKit 和 Swift UI 时创建自定义更新UIViewController 行为?
How to create custom updateUIViewController behavior when integrating UIKit and Swift UI?
我有一个如下所述的视图控制器。
//
// 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)
}
在我的子视图中,我有按钮可以在堆栈中前后移动:
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")
}
}
}
}
前进按钮行为正确,协调器调用激活 .forward
动画样式的 updateUIViewController。问题是后退按钮在应该调用前进时也会显示前进动画样式。
我试过几种方法来解决这个问题,但 none 其中确实有效。
我真正想要的是创建一个显式触发前进或后退更新流程的扩展。有什么想法可以实现吗?
提前致谢
在我看来你有一个
@State var currentPage: Int = 0
您用于许多 UIHostingController/mySubView 并且也在 PageViewController 中使用。尝试使用不同的(数组)
每次使用的变量。
我在完全相同的示例中遇到了完全相同的问题。
我通过将可见 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)
}
我有一个如下所述的视图控制器。
//
// 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)
}
在我的子视图中,我有按钮可以在堆栈中前后移动:
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")
}
}
}
}
前进按钮行为正确,协调器调用激活 .forward
动画样式的 updateUIViewController。问题是后退按钮在应该调用前进时也会显示前进动画样式。
我试过几种方法来解决这个问题,但 none 其中确实有效。 我真正想要的是创建一个显式触发前进或后退更新流程的扩展。有什么想法可以实现吗?
提前致谢
在我看来你有一个
@State var currentPage: Int = 0
您用于许多 UIHostingController/mySubView 并且也在 PageViewController 中使用。尝试使用不同的(数组) 每次使用的变量。
我在完全相同的示例中遇到了完全相同的问题。
我通过将可见 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)
}