【发布时间】:2021-07-23 07:57:34
【问题描述】:
我正在尝试在他们的 iOS 应用程序中创建一个类似于 Pinterest 上下文菜单的上下文菜单。长按帖子会显示一个四按钮视图,当用户继续长按时,可以拖动并选择其他按钮。松开长按将选择您当前选择的任何按钮,或者如果您没有选择任何内容,则完全关闭菜单。请参阅下面的示例:
到目前为止,我在这里尝试过类似于 Apple 文档的内容: https://developer.apple.com/documentation/swiftui/longpressgesture
但似乎手势一旦达到手势中定义的 minimumDuration 就结束了。我希望手势在用户按住时一直持续,并在他们松开时立即结束。
此外,当涉及到拖动和选择其他按钮时,我感到很困惑。到目前为止,这是我的方法:
struct Example: View {
@GestureState var isDetectingLongPress = false
@State var completedLongPress = false
var longPress: some Gesture {
LongPressGesture(minimumDuration: 3)
.updating($isDetectingLongPress) { currentState, gestureState,
transaction in
gestureState = currentState
transaction.animation = Animation.easeIn(duration: 2.0)
}
.onEnded { finished in
self.completedLongPress = finished
}
}
var body: some View {
HStack {
Spacer()
ZStack {
// Three button array to fan out when main button is being held
Button(action: {
// ToDo
}) {
Image(systemName: "circle.fill")
.frame(width: 70, height: 70)
.foregroundColor(.red)
}
.offset(x: self.isDetectingLongPress ? -90 : 0, y: self.isDetectingLongPress ? -90 : 0)
Button(action: {
// ToDo
}) {
Image(systemName: "circle.fill")
.frame(width: 70, height: 70)
.foregroundColor(.green)
}
.offset(x: 0, y: self.isDetectingLongPress ? -120 : 0)
Button(action: {
// ToDo
}) {
Image(systemName: "circle.fill")
.frame(width: 70, height: 70)
.foregroundColor(.blue)
}
.offset(x: self.isDetectingLongPress ? 90 : 0, y: self.isDetectingLongPress ? -90 : 0)
// Main button
Image(systemName: "largecircle.fill.circle")
.gesture(longPress)
}
Spacer()
}
}
【问题讨论】:
-
@Yrb 我也遇到过这个问题。不幸的是,它并没有像我希望的那样工作。看起来我可能需要尝试使用 UIKit 来执行此操作并将其包装为 SwiftUI。
标签: ios swift swiftui menu pinterest