В приведенном ниже коде я хочу позволить пользователю позже редактировать свойства выбранного местоположения в новом представлении в виде модификатора представления листа, прикрепленного к карте. Это не запускается, когда я долго нажимаю на ранее выбранное место. < /P>
import SwiftUI
import MapKit
struct ContentView: View {
@State private var userLocations = [UserLocation]()
@State private var selectedPlace: UserLocation? = nil
let startPosition = MapCameraPosition.region(
MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 42.196, longitude: 24.747),
span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10)
)
)
var body: some View {
MapReader { proxy in
Map(initialPosition: startPosition) {
ForEach(userLocations) { location in
Annotation(location.name, coordinate: location.coordinate) {
Image(systemName: "star.circle")
.resizable()
.foregroundStyle(.red)
.frame(width: 44, height: 44)
.background(.white)
.clipShape(Circle())
.onLongPressGesture {
selectedPlace = location
}
}
}
}
.onTapGesture { position in
if let coordinate = proxy.convert(position, from: .local) {
let newLocation = UserLocation(
id: UUID(),
name: "New Location",
description: "",
latitude: coordinate.latitude,
longitude: coordinate.longitude
)
userLocations.append(newLocation)
}
}
.sheet(item: $selectedPlace) { place in
Text(place.name)
}
}
}
}
< /code>
ниже находится структура данных местоположения в отдельном Fift File < /p>
import Foundation
import MapKit
struct UserLocation: Codable, Equatable, Identifiable {
let id: UUID
var name: String
var description: String
var latitude: Double
var longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
}
< /code>
Я добавил отпечатки в местах действия (Ontapgesture, OnlongpressGesture). Печать в Ontapgesture выполняется, а местоположения действительно отображаются при выборе. Это означает, что LongpressGesture не запускается при длинной прессе. И это за пределы моего понимания на данный момент. Заранее спасибо за вашу помощь.
Подробнее здесь: https://stackoverflow.com/questions/793 ... ongpress-x