ios - SwiftUI 和三指撤消手势
<p><p>我正在尝试在 iOS 的 SwiftUI 应用程序中实现撤消,但我无法让撤消手势起作用。这是一个演示问题的示例:</p>
<pre class="lang-swift prettyprint-override"><code>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)
})
}
}
}
</code></pre>
<p>切换开关然后点击撤消按钮可以正常工作。使用三指撤消手势或摇晃撤消不会执行任何操作。你如何绑定(bind)系统手势?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>似乎编辑手势要求窗口具有第一响应者,并且 SwiftUI 没有设置 <code>UIWindow</code> 默认选择作为第一响应者的任何内容。</p>
<p>如果您将 <code>UIHostingController</code> 子类化,并且在您的子类中,您会覆盖 <code>canBecomeFirstResponder</code> 以返回 <code>true</code>,然后是 <code>UIWindow</code> 默认情况下会将您的 Controller 设置为第一响应者,这似乎足以启用编辑手势。</p>
<p>我在运行 iPadOS 13.1 beta 2 (17A5831c) 的 iPad Pro 上测试了以下代码。它主要工作。我相信有一个 iOS 错误,可能在较新的测试版中修复:当撤消堆栈为空时,手势有时不起作用(即使重做操作是可能的)。切换到主屏幕,然后返回测试应用程序(不终止测试应用程序)似乎使编辑手势再次起作用。</p>
<pre><code>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()
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - SwiftUI 和三指撤消手势,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/57931860/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/57931860/
</a>
</p>
页:
[1]