我试图防止出现警报后出现“向后滑动”动画。以下是可重现的代码:
struct ContentView: View {
@State private var showingAlert = false
var body: some View {
List {
Text("Archive")
.swipeActions(edge: .trailing, allowsFullSwipe: false) {
Button {
showingAlert = true
} label: {
Image(systemName: "archivebox.fill")
}
.tint(.yellow)
}
.alert("Archive?", isPresented: $showingAlert) {
Button("Archive") {
showingAlert = true
}
Button("Cancel", role: .cancel) {
showingAlert = false
}
} message: {
Text("Some message")
}
}
}
}
它看起来是这样的:
但我希望该行保持在原位,以便存档按钮可见,并在用户交互时以动画方式返回(如果他们选择取消或存档)。我想这可能吗?
在显示滑动操作时显示警报的一种方法是用覆盖层覆盖操作按钮,并使用它来触发警报。
GeometryReader
可用于检测行何时向左偏移。这是一种非常可靠的检测滑动动作何时显示的方法。.matchedGeometryEffect
。