![](/img/trans.png)
[英]How to fire event handler when the user STOPS a Long Press Gesture in SwiftUI?
[英]How to run function when long press gesture detected and stopped?
我想實現以下示例:用戶需要按下一個按鈕至少 0.3 秒長,此后錄音開始,用戶松開按鈕后錄音停止。
我的解決方案是:
Image(systemName: "mic.circle")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 70)
.foregroundColor(Color.blue)
.onLongPressGesture(minimumDuration: 0.3){
print("start recording...")
}
當用戶停止按下按鈕時,如何實現 function?
我可以解決我的問題,希望它對你們中的一些人有用:
@State private var isPressingDown: Bool = false
Image(systemName: "mic.circle")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 70)
.foregroundColor(Color.blue)
.onLongPressGesture(minimumDuration: 0.3){
self.isPressingDown = true
print("started")
}
.simultaneousGesture(
DragGesture(minimumDistance: 0)
.onEnded{ _ in
if self.isPressingDown{
self.isPressingDown = false
print("ended")
}
}
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.