我正在尝试实现一个长按按钮,仅当我按下按钮时才会更改我使用的 SF 符号。我不太确定应该在哪里使用这些图像。问题是,当我停止按下时,按钮就会被触发。有没有办法不激活它(按下时除外)?
我正在寻找的是一个按钮:
When pressed持续 2 秒,符号changes,在那 2 秒之后,当released,符号返回到初始状态.
struct heartButton: View {
@State private var isDetectingPress = false
var body: some View {
VStack {
Image(systemName: isDetectingPress ? "heart.fill" : "heart").font(.system(size: 16, weight: .regular))
Button(action: {
self.isDetectingPress.toggle()
}) {
Text("Button")
}
}
}
这是一个解决方案。使用 Xcode 11.5b 进行测试。
struct heartButton: View {
@GestureState private var isDetectingPress = false
var body: some View {
VStack {
Image(systemName: isDetectingPress ? "heart.fill" : "heart").font(.system(size: 16, weight: .regular))
Text("Press")
.gesture(LongPressGesture(minimumDuration: 2).sequenced(before: DragGesture(minimumDistance: 0, coordinateSpace: .local)).updating($isDetectingPress) { value, state, _ in
switch value {
case .second(true, nil):
state = true
default:
break
}
})
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)