[英]SwiftUI: How to run action when user releases screen from a longPressGesture?
[英]How to fail a LongPressGesture on SwiftUI
當我將手指從圖像上移開時,我試圖用 maximumDistance 使 LongPressGesture 失敗,但這不起作用,它會一直打印消息“Pressed”
struct ContentView: View {
@GestureState private var isDetectingPress = false
var body: some View {
Image(systemName: "trash")
.resizable().aspectRatio(contentMode: .fit)
.frame(width: 100, height: 100)
.scaleEffect(isDetectingPress ? 0.5 : 1)
.animation(.easeInOut(duration: 0.2))
.gesture(LongPressGesture(minimumDuration: 0.01, maximumDistance: 10).sequenced(before:DragGesture(minimumDistance: 0).onEnded {_ in
print("Pressed")
})
.updating($isDetectingPress) { value, state, _ in
switch value {
case .second(true, nil):
state = true
default:
break
}
})
}
}
更改updating
修飾符以檢測是否存在拖動量:
.updating($isDetectingPress) { value, state, _ in
switch value {
case .second(true, nil):
state = true
case .second(true, _): // add this case to handle `non-nil` drag amount
state = false
default:
break
}
並在DragGesture
本身中為DragGesture
設置最小距離(例如 100):
DragGesture(minimumDistance: 100)
而不是LongPressGesture
:
//LongPressGesture(minimumDuration: 0.01, maximumDistance: 100) // remove `maximumDistance`
LongPressGesture(minimumDuration: 0.01)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.