如何运行功能时,长按手势检测和停止?



我想实现以下示例:用户需要按一个按钮至少0.3秒长,在此之后音频录制开始,并在用户释放按钮后录制停止。

我的解决方案是:

Image(systemName: "mic.circle")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 70)
.foregroundColor(Color.blue)
.onLongPressGesture(minimumDuration: 0.3){
print("start recording...")
}

如何实现当用户停止按下按钮时的功能?

我可以解决我的问题,希望对你们中的一些人有用:

@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")
}
}
)

最新更新