swift - 如何在 SwiftUI 上使 LongPressGesture 失败
问题描述
当我将手指从图像上移开时,我试图用 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
}
并为其自身设置一个最小距离(例如 100 DragGesture
)DragGesture
:
DragGesture(minimumDistance: 100)
而不是LongPressGesture
:
//LongPressGesture(minimumDuration: 0.01, maximumDistance: 100) // remove `maximumDistance`
LongPressGesture(minimumDuration: 0.01)
推荐阅读
- c# - AMO 获取处理数据但不处理索引的分区
- c++ - 调用未知构造函数而不是移动构造函数
- django - Django 创建动态表单,查询集的值作为字段
- html - Bootstrap3:定义固定宽度的列,连续超过 12 列
- angular - 访问模板中组件的对象属性不会显示该对象的属性
- javascript - 如何使用 vanilla javascript 将 html 表中的数据转换为对象数组
- java - 如何将地图列表转换为xml
- docker - 在 Docker 环境中实现 Consul 健康检查
- javascript - chrome 和 firefox 中新标签的 Blob 名称问题
- ios - 如果从 Siri 快捷方式执行操作,我应该捐赠交互吗?