OStack程序员社区-中国程序员成长平台

标题: ios - SwiftUI 和三指撤消手势 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 05:41
标题: ios - SwiftUI 和三指撤消手势

我正在尝试在 iOS 的 SwiftUI 应用程序中实现撤消,但我无法让撤消手势起作用。这是一个演示问题的示例:

class Model: ObservableObject {
    @Published var active = false

    func registerUndo(_ newValue: Bool, in undoManager: UndoManager?) {
        let oldValue = active
        undoManager?.registerUndo(withTarget: self) { target in
            target.active = oldValue
        }
        active = newValue
    }
}

struct UndoTest: View {
    @ObservedObject private var model = Model()
    @Environment(\.undoManager) var undoManager

    var body: some View {
        VStack {
            Toggle(isOn: Binding<Bool>(
                get: { self.model.active },
                set: { self.model.registerUndo($0, in: self.undoManager) }
            )) {
                Text("Toggle")
            }
            .frame(width: 120)
            Button(action: {
                self.undoManager?.undo()
            }, label: {
                Text("Undo")
                    .foregroundColor(.white)
                    .padding()
                    .background(self.undoManager?.canUndo == true ? Color.blue : Color.gray)
            })
        }
    }
}

切换开关然后点击撤消按钮可以正常工作。使用三指撤消手势或摇晃撤消不会执行任何操作。你如何绑定(bind)系统手势?



Best Answer-推荐答案


似乎编辑手势要求窗口具有第一响应者,并且 SwiftUI 没有设置 UIWindow 默认选择作为第一响应者的任何内容。

如果您将 UIHostingController 子类化,并且在您的子类中,您会覆盖 canBecomeFirstResponder 以返回 true,然后是 UIWindow 默认情况下会将您的 Controller 设置为第一响应者,这似乎足以启用编辑手势。

我在运行 iPadOS 13.1 beta 2 (17A5831c) 的 iPad Pro 上测试了以下代码。它主要工作。我相信有一个 iOS 错误,可能在较新的测试版中修复:当撤消堆栈为空时,手势有时不起作用(即使重做操作是可能的)。切换到主屏幕,然后返回测试应用程序(不终止测试应用程序)似乎使编辑手势再次起作用。

import UIKit
import SwiftUI

class MyHostingController<Content: View>: UIHostingController<Content> {
    override var canBecomeFirstResponder: Bool { true }
}

class Model: ObservableObject {
    init(undoManager: UndoManager) {
        self.undoManager = undoManager
    }

    let undoManager: UndoManager

    @Published var active = false {
        willSet {
            let oldValue = active
            undoManager.registerUndo(withTarget: self) { me in
                me.active = oldValue
            }
        }
    }
}

struct ContentView: View {
    @ObservedObject var model: Model
    @Environment(\.undoManager) var undoManager

    var body: some View {
        VStack {
            Toggle("Active", isOn: $model.active)
                .frame(width: 120)
            HStack {
                Button("Undo") {
                    withAnimation {
                        self.undoManager?.undo()
                    }
                }.disabled(!(undoManager?.canUndo ?? false))
                Button("Redo") {
                    withAnimation {
                        self.undoManager?.redo()
                    }
                }.disabled(!(undoManager?.canRedo ?? false))
            }
        }
    }
}

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else { return }

        let window = UIWindow(windowScene: scene)
        let model = Model(undoManager: window.undoManager!)
        let contentView = ContentView(model: model)

        window.rootViewController = MyHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
    }
}

关于ios - SwiftUI 和三指撤消手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57931860/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4