我在我的 SwiftUI 代碼中使用 DragGesture()..它作業正常..但是對于一個邏輯我想知道它是向下拖動還是向上拖動,有沒有辦法檢測到這一點?
let drag = DragGesture()
.onChanged { value in
//some logic
}
.onEnded { value in
// some logic
expand.toggle()
}
uj5u.com熱心網友回復:
一種可能的方法是使用額外的 state 屬性來存盤
value.location.y坐標和更新從屬狀態,比較之前存盤的和當前在 drag 中接收的狀態value:
使用 Xcode 13.3 / iOS 15.4 測驗

這是主要部分:
.gesture(DragGesture()
.onChanged { value in
let newValue = value.location.y // fetch prev !!
if let prev = self.current {
self.isUp = prev > newValue // << here !!
}
self.current = newValue // store curr !!
self.offsetY = value.translation.height
}
完整的發現和代碼在這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/464490.html
