Swift - detailCalloutAccessoryView не обновляет данные
У меня есть пользовательская аннотация для моего mapView. Сначала я установил для него координаты, заголовок (например, "первый заголовок"), subTitle (например, "первый адрес"), userId и расстояние (например, 0 метров) с некоторыми данными. Я добавляю его в mapView и в массив для последующего использования. Все работает, это показывает на mapView, я нажимаю его, и выноска показывает, что исходные данные.
Позже мне сообщают, что местоположение этой выноски изменилось. Я перебираю массив и обновляю выноску новыми данными для свойств координат, заголовка (например, "новый заголовок"), подзаголовка (например, "новый адрес") и расстояния (например, 100 метров). Я также анимирую выноску из исходного местоположения в новое местоположение. Анимация работает отлично, и выноска перемещается из точки А в точку Б.
Проблема в том, что, когда я нажимаю на аннотацию, старые данные отображаются в выноске вместо новых данных.
я использую calloutAccessoryControlTapped
нажать на новый VC. Когда я ставлю точку останова, пользовательский пин-код содержит все новые данные. Кажется, ошибка произошла с выноской.
Как это исправить?
Я не хочу удалять все комментарии из mapView, так что это не вариант. Я звоню mapView.removeAnnotation(customPin)
а также mapView.addAnnotation(customPin)
которая устраняет проблему для этого вывода, но мигает, когда вывод удален и добавлен обратно на карту, а затем, когда он анимируется на новое место, он выглядит прерывистым.
Пользовательская аннотация
class CustomPin: NSObject, MKAnnotation {
@objc dynamic var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
var userId: String?
var distance: CLLocationDistance?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, userId: String, distance: CLLocationDistance?) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
self.userId = userId
self.distance = distance
super.init()
}
}
Первый раз аннотацию устанавливают с начальными данными
firstFunctionThatGetsTheInitialLocation(origLat, origLon) {
let firstCoordinate = CLLocationCoordinate2DMake(origLat, origLon)
let distanceInMeters: CLLocationDistance = self.center.distance(from: anotherUsersLocation)
let customPin = CustomPin(coordinate: firstCoordinate, title: "first title", subtitle: "first address", userId: "12345", distance: distance)
DispatchQueue.main.async { [weak self] in
self?.mapView.addAnnotation(customPin)
self?.arrOfPins.append(customPin)
}
}
Во второй раз аннотации устанавливаются с новыми данными
secondFunctionThatGetsTheNewLocation(newCoordinate: CLLocationCoordinate2D, newDistance: CLLocationDistance) {
for pin in customPins {
pin.title = "second title" // ** updates but the callout doesn't reflect it
pin.subTitle = "second address" // ** updates but the callout doesn't reflect it
pin.distance = newDistance // ** updates but the callout doesn't reflect it
// calling these gives me the new data but the annotation blinks and moves really fast to it's new location
// mapView.removeAnnotation(pin)
// mapView.addAnnotation(pin)
UIView.animate(withDuration: 1) {
pin.coordinate = newCoordinate // this updates and animates to the new location with no problem
}
}
}
MapView view для аннотации
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation.isKind(of: MKUserLocation.self) { return nil }
guard let annotation = annotation as? CustomPin else { return nil }
let reuseIdentifier = "CustomPin"
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView?.canShowCallout = true
annotationView?.calloutOffset = CGPoint(x: -5, y: 5)
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
annotationView?.image = UIImage(named: "chevronImage")
} else {
annotationView?.annotation = annotation
}
annotationView?.detailCalloutAccessoryView = nil
annotationView?.detailCalloutAccessoryView = createCallOutWithDataFrom(customPin: annotation)
return annotationView
}
Создание UIView для выноски
func createCallOutWithDataFrom(customPin: CustomPin) -> UIView {
let titleText = customPin.title
let subTitleText = customPin.subTitle
let distanceText = subTitle.distance // gets converted to a string
// 1. create a UIView
// 2. create some labels and add the text from the title, subTitle, and distance and add them as subViews to the UIView
// 3. return the UIView
}
1 ответ
Есть несколько вопросов:
Вам нужно использовать
@objc dynamic
классификатор для любых свойств, которые вы хотите наблюдать. Стандартный вызов выполняет наблюдение значения ключа (KVO) наtitle
а такжеsubtitle
, (И представление аннотации наблюдает за изменениями вcoordinate
.)Если вы хотите наблюдать
userid
а такжеdistance
, ты должен сделать это@objc dynamic
также. Обратите внимание, вам придется сделатьdistance
быть необязательным, чтобы сделать это наблюдаемым:var distance: CLLocationDistance
Так:
class CustomAnnotation: NSObject, MKAnnotation { // standard MKAnnotation properties @objc dynamic var coordinate: CLLocationCoordinate2D @objc dynamic var title: String? @objc dynamic var subtitle: String? // additional custom properties @objc dynamic var userId: String @objc dynamic var distance: CLLocationDistance init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, userId: String, distance: CLLocationDistance) { self.userId = userId self.distance = distance self.coordinate = coordinate self.title = title self.subtitle = subtitle super.init() } }
Как я уже сказал, стандартная выноска соблюдает
title
а такжеsubtitle
, В то время как вы должны сделать заметные свойства заметными, если вы собираетесь создавать свои собственныеdetailCalloutAccessoryView
вам придется сделать свое собственное КВО:class CustomAnnotationView: MKMarkerAnnotationView { private let customClusteringIdentifier = "..." override init(annotation: MKAnnotation?, reuseIdentifier: String?) { super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) canShowCallout = true detailCalloutAccessoryView = createCallOutWithDataFrom(customAnnotation: annotation as? CustomAnnotation) clusteringIdentifier = customClusteringIdentifier } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } deinit { removeAnyObservers() } override var annotation: MKAnnotation? { didSet { removeAnyObservers() clusteringIdentifier = customClusteringIdentifier if let customAnnotation = annotation as? CustomAnnotation { updateAndAddObservers(for: customAnnotation) } } } private var subtitleObserver: NSKeyValueObservation? private var userObserver: NSKeyValueObservation? private var distanceObserver: NSKeyValueObservation? private let subtitleLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false return label }() private let userLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false return label }() private let distanceLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false return label }() } private extension CustomAnnotationView { func updateAndAddObservers(for customAnnotation: CustomAnnotation) { subtitleLabel.text = customAnnotation.subtitle subtitleObserver = customAnnotation.observe(\.subtitle) { [weak self] customAnnotation, _ in self?.subtitleLabel.text = customAnnotation.subtitle } userLabel.text = customAnnotation.userId userObserver = customAnnotation.observe(\.userId) { [weak self] customAnnotation, _ in self?.userLabel.text = customAnnotation.userId } distanceLabel.text = "\(customAnnotation.distance) meters" distanceObserver = customAnnotation.observe(\.distance) { [weak self] customAnnotation, _ in self?.distanceLabel.text = "\(customAnnotation.distance) meters" } } func removeAnyObservers() { subtitleObserver = nil userObserver = nil distanceObserver = nil } func createCallOutWithDataFrom(customAnnotation: CustomAnnotation?) -> UIView { let view = UIView() view.translatesAutoresizingMaskIntoConstraints = false view.addSubview(subtitleLabel) view.addSubview(userLabel) view.addSubview(distanceLabel) NSLayoutConstraint.activate([ subtitleLabel.topAnchor.constraint(equalTo: view.topAnchor), subtitleLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor), subtitleLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor), subtitleLabel.bottomAnchor.constraint(equalTo: userLabel.topAnchor), userLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor), userLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor), userLabel.bottomAnchor.constraint(equalTo: distanceLabel.topAnchor), distanceLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor), distanceLabel.trailingAnchor.constraint(equalTo: view.trailingAnchor), distanceLabel.bottomAnchor.constraint(equalTo: view.bottomAnchor) ]) if let customAnnotation = customAnnotation { updateAndAddObservers(for: customAnnotation) } return view } }