SwiftUI LongPressGesture 需要很长时间才能识别 TapGesture 是否也存在
SwiftUI LongPressGesture takes too long to recognize when TapGesture also present
我想识别同一商品上的 TapGesture
和 LongPressGesture
。它工作正常,但有以下例外:LongPressGesture
在我指定的持续时间后单独响应,即 0.25 秒,但是当我将它与 TapGesture
组合时,它至少需要 1 秒——我找不到让它响应更快的方法。这是一个演示:
这是它的代码:
struct ContentView: View {
@State var message = ""
var body: some View {
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150)
.onTapGesture(count: 1) {
message = "TAP"
}
.onLongPressGesture(minimumDuration: 0.25) {
message = "LONG\nPRESS"
}
.overlay(Text(message)
.font(.title).bold()
.multilineTextAlignment(.center)
.allowsHitTesting(false))
}
}
请注意,它工作正常,除了 LongPress 的持续时间,它比 0.25 秒长得多。
有什么想法吗?提前致谢!
为了在项目中有一些适合每个人需要的多手势,Apple 除了正常手势之外没有什么可以提供的,将它们混合在一起以达到想要的手势有时会很棘手,这是一个救赎,工作 没有 问题或错误!
这里有一个名为 interactionReader 的自定义零问题 手势,我们可以将其应用于任何视图。因为 LongPressGesture 和 TapGesture 同时。
import SwiftUI
struct ContentView: View {
var body: some View {
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150)
.interactionReader(longPressSensitivity: 250, tapAction: tapAction, longPressAction: longPressAction, scaleEffect: true)
.animation(Animation.easeInOut(duration: 0.2))
}
func tapAction() { print("tap action!") }
func longPressAction() { print("longPress action!") }
}
struct InteractionReaderViewModifier: ViewModifier {
var longPressSensitivity: Int
var tapAction: () -> Void
var longPressAction: () -> Void
var scaleEffect: Bool = true
@State private var isPressing: Bool = Bool()
@State private var currentDismissId: DispatchTime = DispatchTime.now()
@State private var lastInteractionKind: String = String()
func body(content: Content) -> some View {
let processedContent = content
.gesture(gesture)
.onChange(of: isPressing) { newValue in
currentDismissId = DispatchTime.now() + .milliseconds(longPressSensitivity)
let dismissId: DispatchTime = currentDismissId
if isPressing {
DispatchQueue.main.asyncAfter(deadline: dismissId) {
if isPressing { if (dismissId == currentDismissId) { lastInteractionKind = "longPress"; longPressAction() } }
}
}
else {
if (lastInteractionKind != "longPress") { lastInteractionKind = "tap"; tapAction() }
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(50)) {lastInteractionKind = "none"}
}
}
return Group {
if scaleEffect { processedContent.scaleEffect(lastInteractionKind == "longPress" ? 1.5: (lastInteractionKind == "tap" ? 0.8 : 1.0 )) }
else { processedContent }
}
}
var gesture: some Gesture {
DragGesture(minimumDistance: 0.0, coordinateSpace: .local)
.onChanged() { _ in if !isPressing { isPressing = true } }
.onEnded() { _ in isPressing = false }
}
}
extension View {
func interactionReader(longPressSensitivity: Int, tapAction: @escaping () -> Void, longPressAction: @escaping () -> Void, scaleEffect: Bool = true) -> some View {
return self.modifier(InteractionReaderViewModifier(longPressSensitivity: longPressSensitivity, tapAction: tapAction, longPressAction: longPressAction, scaleEffect: scaleEffect))
}
}
LongPressGesture 有另一个完成功能,可以在用户第一次按下时执行一个动作……并以最短持续时间再次执行。这样的东西能行吗?
struct LongTapTest: View {
@State var message = ""
var body: some View {
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150)
.onLongPressGesture(minimumDuration: 0.25, maximumDistance: 50, pressing: { (isPressing) in
if isPressing {
// called on touch down
} else {
// called on touch up
message = "TAP"
}
}, perform: {
message = "LONG\nPRESS"
})
.overlay(Text(message)
.font(.title).bold()
.multilineTextAlignment(.center)
.allowsHitTesting(false))
}
}
好吧,这并不漂亮,但效果很好。它记录每个 tap/press 的开头,如果它在 0.25 秒之前结束,则将其视为 TapGesture
,否则将其视为 LongPressGesture
:
struct ContentView: View {
@State var pressInProgress = false
@State var gestureEnded = false
@State var workItem: DispatchWorkItem? = nil
@State var message = ""
var body: some View {
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150, alignment: .center)
.overlay(Text(message)
.font(.title).bold()
.multilineTextAlignment(.center)
.allowsHitTesting(false))
.gesture(
DragGesture(minimumDistance: 0, coordinateSpace: .global)
.onChanged { _ in
guard !pressInProgress else { return }
pressInProgress = true
workItem = DispatchWorkItem {
message = "LONG\nPRESSED"
gestureEnded = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: workItem!)
}
.onEnded { _ in
pressInProgress = false
workItem?.cancel()
guard !gestureEnded else { // moot if we're past 0.25 seconds so ignore
gestureEnded = false
return
}
message = "TAPPED"
gestureEnded = false
}
)
}
}
如果有人能想到一个实际使用 LongPressGesture
和 TapGesture
的解决方案,我会更喜欢!
我想识别同一商品上的 TapGesture
和 LongPressGesture
。它工作正常,但有以下例外:LongPressGesture
在我指定的持续时间后单独响应,即 0.25 秒,但是当我将它与 TapGesture
组合时,它至少需要 1 秒——我找不到让它响应更快的方法。这是一个演示:
这是它的代码:
struct ContentView: View {
@State var message = ""
var body: some View {
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150)
.onTapGesture(count: 1) {
message = "TAP"
}
.onLongPressGesture(minimumDuration: 0.25) {
message = "LONG\nPRESS"
}
.overlay(Text(message)
.font(.title).bold()
.multilineTextAlignment(.center)
.allowsHitTesting(false))
}
}
请注意,它工作正常,除了 LongPress 的持续时间,它比 0.25 秒长得多。
有什么想法吗?提前致谢!
为了在项目中有一些适合每个人需要的多手势,Apple 除了正常手势之外没有什么可以提供的,将它们混合在一起以达到想要的手势有时会很棘手,这是一个救赎,工作 没有 问题或错误!
这里有一个名为 interactionReader 的自定义零问题 手势,我们可以将其应用于任何视图。因为 LongPressGesture 和 TapGesture 同时。
import SwiftUI
struct ContentView: View {
var body: some View {
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150)
.interactionReader(longPressSensitivity: 250, tapAction: tapAction, longPressAction: longPressAction, scaleEffect: true)
.animation(Animation.easeInOut(duration: 0.2))
}
func tapAction() { print("tap action!") }
func longPressAction() { print("longPress action!") }
}
struct InteractionReaderViewModifier: ViewModifier {
var longPressSensitivity: Int
var tapAction: () -> Void
var longPressAction: () -> Void
var scaleEffect: Bool = true
@State private var isPressing: Bool = Bool()
@State private var currentDismissId: DispatchTime = DispatchTime.now()
@State private var lastInteractionKind: String = String()
func body(content: Content) -> some View {
let processedContent = content
.gesture(gesture)
.onChange(of: isPressing) { newValue in
currentDismissId = DispatchTime.now() + .milliseconds(longPressSensitivity)
let dismissId: DispatchTime = currentDismissId
if isPressing {
DispatchQueue.main.asyncAfter(deadline: dismissId) {
if isPressing { if (dismissId == currentDismissId) { lastInteractionKind = "longPress"; longPressAction() } }
}
}
else {
if (lastInteractionKind != "longPress") { lastInteractionKind = "tap"; tapAction() }
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(50)) {lastInteractionKind = "none"}
}
}
return Group {
if scaleEffect { processedContent.scaleEffect(lastInteractionKind == "longPress" ? 1.5: (lastInteractionKind == "tap" ? 0.8 : 1.0 )) }
else { processedContent }
}
}
var gesture: some Gesture {
DragGesture(minimumDistance: 0.0, coordinateSpace: .local)
.onChanged() { _ in if !isPressing { isPressing = true } }
.onEnded() { _ in isPressing = false }
}
}
extension View {
func interactionReader(longPressSensitivity: Int, tapAction: @escaping () -> Void, longPressAction: @escaping () -> Void, scaleEffect: Bool = true) -> some View {
return self.modifier(InteractionReaderViewModifier(longPressSensitivity: longPressSensitivity, tapAction: tapAction, longPressAction: longPressAction, scaleEffect: scaleEffect))
}
}
LongPressGesture 有另一个完成功能,可以在用户第一次按下时执行一个动作……并以最短持续时间再次执行。这样的东西能行吗?
struct LongTapTest: View {
@State var message = ""
var body: some View {
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150)
.onLongPressGesture(minimumDuration: 0.25, maximumDistance: 50, pressing: { (isPressing) in
if isPressing {
// called on touch down
} else {
// called on touch up
message = "TAP"
}
}, perform: {
message = "LONG\nPRESS"
})
.overlay(Text(message)
.font(.title).bold()
.multilineTextAlignment(.center)
.allowsHitTesting(false))
}
}
好吧,这并不漂亮,但效果很好。它记录每个 tap/press 的开头,如果它在 0.25 秒之前结束,则将其视为 TapGesture
,否则将其视为 LongPressGesture
:
struct ContentView: View {
@State var pressInProgress = false
@State var gestureEnded = false
@State var workItem: DispatchWorkItem? = nil
@State var message = ""
var body: some View {
Circle()
.fill(Color.yellow)
.frame(width: 150, height: 150, alignment: .center)
.overlay(Text(message)
.font(.title).bold()
.multilineTextAlignment(.center)
.allowsHitTesting(false))
.gesture(
DragGesture(minimumDistance: 0, coordinateSpace: .global)
.onChanged { _ in
guard !pressInProgress else { return }
pressInProgress = true
workItem = DispatchWorkItem {
message = "LONG\nPRESSED"
gestureEnded = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: workItem!)
}
.onEnded { _ in
pressInProgress = false
workItem?.cancel()
guard !gestureEnded else { // moot if we're past 0.25 seconds so ignore
gestureEnded = false
return
}
message = "TAPPED"
gestureEnded = false
}
)
}
}
如果有人能想到一个实际使用 LongPressGesture
和 TapGesture
的解决方案,我会更喜欢!