I'm trying to implement undo in a SwiftUI app for iOS, but I haven't been able to get the undo gestures to work. Here's a sample that demonstrates the problem:
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)
})
}
}
}
Switching the toggle around then tapping the undo button works fine. Using the three-finger undo gesture or shaking to undo does nothing. How do you tie in to the system gesture?