Это то, с чего я начал
Код: Выделить всё
struct ExampleView: View {
@State var showCustomContextMenu = false
@State var longPressLocation = CGPoint.zero
var body: some View {
Rectangle()
.foregroundColor(Color.green)
.frame(width: 100.0, height: 100.0)
.onLongPressGesture {
print("OnLongPressGesture")
self.showCustomContextMenu = true
}
.overlay(
Rectangle()
.foregroundColor(Color.red)
.frame(width: 50.0, height: 50.0)
.position(longPressLocation) // Void)
func makeUIView(context: UIViewRepresentableContext) -> TapView.UIViewType {
let v = UIView(frame: .zero)
let gesture = SingleTouchDownGestureRecognizer(target: context.coordinator,
action: #selector(Coordinator.tapped))
v.addGestureRecognizer(gesture)
return v
}
class Coordinator: NSObject {
var tappedCallback: ((CGPoint) -> Void)
init(tappedCallback: @escaping ((CGPoint) -> Void)) {
self.tappedCallback = tappedCallback
}
@objc func tapped(gesture:UITapGestureRecognizer) {
self.tappedCallback(gesture.location(in: gesture.view))
}
}
func makeCoordinator() -> TapView.Coordinator {
return Coordinator(tappedCallback:self.tappedCallback)
}
func updateUIView(_ uiView: UIView,
context: UIViewRepresentableContext) {
}
}
class SingleTouchDownGestureRecognizer: UIGestureRecognizer {
override func touchesBegan(_ touches: Set, with event: UIEvent) {
if self.state == .possible {
self.state = .recognized
}
}
override func touchesMoved(_ touches: Set, with event: UIEvent) {
self.state = .failed
}
override func touchesEnded(_ touches: Set, with event: UIEvent) {
self.state = .failed
}
}
Другие вопросы SO, которые я рассматривал, но столкнулся с аналогичными проблемами, приведены ниже< /p>
Как обнаружить местоположение жеста касания в SwiftUI?
Swift: Распознаватель жестов при длительном нажатии — обнаружение касаний и длительного нажатия
Возможно, это то, что я ищу, но я не уверен как адаптировать его к SwiftUI.
Подробнее здесь: https://stackoverflow.com/questions/628 ... in-swiftui