gpt4 book ai didi

ios - 如何检测在 onEnded 关闭时是否启动了 sequensed 的第二个手势?

转载 作者:行者123 更新时间:2023-11-29 05:10:49 35 4
gpt4 key购买 nike

我想处理两种手势:简单的短按(通过 DragGesture)和使用另一个 DragGesture 长按。

那么,我如何检测您所做的事情:他/她是否使用了短按或长按?

let sequencedDragGesture = DragGesture(
minimumDistance: 0,
coordinateSpace: .local
).onEnded { value in
print("sequencedDragGesture \(self.touchState)")
}
let minimumLongPressDuration = 0.5
let longPressDrag = LongPressGesture(
minimumDuration: minimumLongPressDuration
)
.sequenced(before: sequencedDragGesture)
.updating($dragState) { value, state, transaction in
print("before \(state)")
switch value {
case .first(true):
state = .pressing
// self.isLongTapActivated = false
case .second(true, let drag):
// self.isLongTapActivated = true
state = .dragging(translation: drag?.translation ?? .zero)
default:
state = .inactive
}
print("updating: \(state), \(self.dragState)")
}
.onChanged { _ in

}
.onEnded { value in
print("onEnded: \(self.touchState)")
guard case .second(true, let drag?) = value else {
return
}

}


private enum DragState {
case inactive
case pressing
case dragging(translation: CGSize)
}

@GestureState private var dragState = DragState.inactive

我无法使用标志isLongTapActivated:

Modifying state during view update, this will cause undefined behavior.

问题是在 onEnded 情况下我收到 .inactive。在 SwiftUI 中可以吗?

我读到了这个:https://developer.apple.com/documentation/swiftui/gestures/composing_swiftui_gestures

最佳答案

我使用类Wrapper找到了解决方案:

final class Wrapper<T> {
var state: T
var isActivated: Bool = false
init(_ state: T) {
self.state = state
}
}

extension Wrapper where T == DragState {
func update(translation: CGSize) {
operationQueue.addOperation {
if self.isActivated {
self.state = .dragging(translation: translation)
} else {
self.state = .pressing(translation: translation)
}
}
}
}

更新了DragState:

// MARK: - DragState

private enum DragState {
case inactive
case pressing(translation: CGSize)
case dragging(translation: CGSize)

var translation: CGSize {
switch self {
case .inactive, .pressing:
return .zero
case .dragging(let translation):
return translation
}
}

var isActive: Bool {
switch self {
case .inactive:
return false
case .pressing, .dragging:
return true
}
}

var isDragging: Bool {
switch self {
case .inactive, .pressing:
return false
case .dragging:
return true
}
}
var isPressing: Bool {
switch self {
case .pressing:
return true
case .inactive, .dragging:
return false
}
}
}

并与另一个“核心”DrawGesture混合。

let sequencedDragGesture = DragGesture(
minimumDistance: 0,
coordinateSpace: .local
)
let minimumLongPressDuration = 0.5
let longPressDrag = LongPressGesture(
minimumDuration: minimumLongPressDuration
)
.sequenced(before: sequencedDragGesture)
.updating($internalDragState) { value, state, transaction in
switch value {
case .first(true):
state = .pressing(translation: .zero)

case .second(true, let drag):
if state.isPressing {
self.wrapper.isActivated = true
self.wrapper.update(translation: drag?.translation ?? .zero)
}


state = .dragging(translation: drag?.translation ?? .zero)
default:
state = .inactive
}
}
let dragGesture = DragGesture(
minimumDistance: 0,
coordinateSpace: .local
)
.onChanged { value in
self.wrapper.update(translation: value.translation)
print("changed: \(value), \(self.wrapper.state)")
}
.onEnded { value in
print("onEnded Drag: \(self.wrapper.state)")
self.wrapper.isActivated = false
self.wrapper.state = .inactive
print("\(self.internalDragState)")
}

let summGesture = longPressDrag
.simultaneously(with: dragGesture)

哪里

@State private var wrapper = Wrapper<DragState>(.inactive)

关于ios - 如何检测在 onEnded 关闭时是否启动了 sequensed 的第二个手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59702086/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com