gpt4 book ai didi

swift - 添加注释后选择带有自定义标注的 MKMarkerAnnotationView

转载 作者:搜寻专家 更新时间:2023-10-31 22:10:44 26 4
gpt4 key购买 nike

我有一个 MKMapView,我想在长按手势上添加注释。添加注释后,我想选择注释 View 。我认为这是一个非常简单的请求。

问题是我正在使用带有自定义 rightCalloutAccessoryView 和自定义 detailCalloutAccessoryView 的新 MKMarkerAnnotationView。目前还没有很好的记录,但是 WWDC 2017 Session 237声明当有多个标题/副标题时将显示标注。

不幸的是,我不是这种情况。当我以编程方式(和手动)选择注释时,我得到一个奇怪的双选状态,我可以在其中看到标注和标记:

selected state

下面是注释 View 代码:

import Foundation
import MapKit

@available(iOS 11.0, *)
protocol TemporaryUserAnnotationViewDelegate: class {
func temporaryUserAnnotationViewDidTapOk(title: String?, userAnnotationView: TemporaryUserAnnotationView)
}

@available(iOS 11.0, *)
class TemporaryUserAnnotationView: MKMarkerAnnotationView {

weak var delegate: TemporaryUserAnnotationViewDelegate?
var textField: UITextField!

init(annotation: TemporaryUserAnnotation) {
super.init(annotation: annotation, reuseIdentifier: TemporaryUserAnnotationMarkerAnnotationView.reuseIdentifier)
markerTintColor = .lbc_blue
canShowCallout = true
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func configure(annotation: TemporaryUserAnnotation) {
let detailView = UIView()
detailView.translatesAutoresizingMaskIntoConstraints = false
textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Titre"
detailView.addSubview(textField)
detailView.heightAnchor.constraint(equalToConstant: 21).isActive = true
detailView.widthAnchor.constraint(equalToConstant: 100).isActive = true
textField.centerXAnchor.constraint(equalTo: detailView.centerXAnchor).isActive = true
textField.centerYAnchor.constraint(equalTo: detailView.centerYAnchor).isActive = true
textField.heightAnchor.constraint(equalToConstant: 21).isActive = true
textField.widthAnchor.constraint(equalToConstant: 100).isActive = true
detailCalloutAccessoryView = detailView

let rightView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 50, height: 70)))
rightView.backgroundColor = .lbc_blue
let button = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 50, height: 40)))
button.setTitle("OK", for: .normal)
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action: #selector(tapRightCalloutAccessoryViewButton), for: .touchUpInside)
rightView.addSubview(button)
rightCalloutAccessoryView = rightView
}

@objc func tapRightCalloutAccessoryViewButton() {
delegate?.temporaryUserAnnotationViewDidTapOk(title: textField.text, userAnnotationView: self)
}
}

这是 Controller 代码:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
print(#function)
if annotation is MKUserLocation { return nil }
if #available(iOS 11.0, *) {
switch annotation {
case let temporarayUserAnnotation as TemporaryUserAnnotation:
if let view = mapView.dequeue() as? TemporaryUserAnnotationView {
view.annotation = annotation
return view
} else {
let view = TemporaryUserAnnotationView(annotation: temporarayUserAnnotation)
view.delegate = self
view.configure(annotation: temporarayUserAnnotation)
return view
}
default:
return nil
}
} else {
let identifier = "marker"
if let view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
view.annotation = annotation
return view
} else {
return MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
}
}

我只是这样添加注释:

func addUserAnnotation(coordinate: CLLocationCoordinate2D) {
let annotation = TemporaryUserAnnotation(coordinate: coordinate)
mapView.addAnnotation(annotation)
mapView.selectAnnotation(annotation, animated: true)
}

我还尝试实现 mapView(_:didAdd:) 方法,但结果在标注前面的标记 View 更糟。

任何帮助将不胜感激!谢谢!

最佳答案

我发现了以下解决方法:添加注释时,请稍等片刻,然后再以编程方式选择它:

func addUserAnnotation(coordinate: CLLocationCoordinate2D) {
let annotation = TemporaryUserAnnotation(coordinate: coordinate)
mapView.addAnnotation(annotation)
DispatchQueue.main.asyncAfter(wallDeadline: .now() + .seconds(1)) {
self.mapView.selectAnnotation(annotation, animated: true)
}
}

我仍然认为这应该不是必需的,所以如果您有更好的解决方案,请随时在这里发布!

关于swift - 添加注释后选择带有自定义标注的 MKMarkerAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46674767/

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