Ant*_*ton 5 duration gesture ios long-press swiftui
我想在同一项目上识别TapGesture
和。LongPressGesture
它工作正常,但有以下例外: the LongPressGesture
alone 在我指定的持续时间后响应,即 0.25 秒,但是当我将它与 结合使用时TapGesture
,至少需要 1 秒\xe2\x80\x94我找不到使其响应更快的方法。这是一个演示:
这是它的代码:
\nstruct ContentView: View {\n @State var message = ""\n\n var body: some View {\n Circle()\n .fill(Color.yellow)\n .frame(width: 150, height: 150)\n .onTapGesture(count: 1) {\n message = "TAP"\n }\n .onLongPressGesture(minimumDuration: 0.25) {\n message = "LONG\\nPRESS"\n }\n .overlay(Text(message)\n .font(.title).bold()\n .multilineTextAlignment(.center)\n .allowsHitTesting(false))\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n请注意,除了长按的持续时间(远长于 0.25 秒)之外,它工作正常。
\n有任何想法吗?提前致谢!
\n为了拥有一些适合项目中每个人需求的多手势,Apple 没有提供比普通手势更好的功能,将它们混合在一起以达到所需的手势有时会很棘手,这是一个救赎,无需工作即可问题或错误!
这里有一个自定义的零问题手势,称为interactionReader,我们可以将其应用到任何View。同时拥有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!") }
}
Run Code Online (Sandbox Code Playgroud)
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 }
}
}
Run Code Online (Sandbox Code Playgroud)
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))
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2380 次 |
最近记录: |