添加注释后选择带有自定义标注的 MKMarkerAnnotationView
Posted
技术标签:
【中文标题】添加注释后选择带有自定义标注的 MKMarkerAnnotationView【英文标题】:Select MKMarkerAnnotationView with custom callout after adding annotation 【发布时间】:2017-10-10 19:47:14 【问题描述】:我有一个 MKMapView,我想在长按手势上添加注释。添加注释后,我想选择注释视图。我假设的请求很简单。
问题是我正在使用带有自定义 rightCalloutAccessoryView 和自定义 detailCalloutAccessoryView 的新 MKMarkerAnnotationView。它还没有很好的记录,但WWDC 2017 Session 237 指出当有多个标题/副标题时将显示标注。
不幸的是,这不是我的情况。当我以编程方式(手动)选择注释时,我得到一个奇怪的双选状态,我可以看到标注和标记:
这里是注解视图代码:
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)
这是控制器代码:
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:)
方法,但在标注前面的标记视图中结果更糟。
任何帮助将不胜感激!谢谢!
【问题讨论】:
【参考方案1】:我找到了以下解决方法:添加注释时,请稍等片刻,然后以编程方式选择它:
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)
我仍然认为这应该没有必要,所以如果您有更好的解决方案,请随时在此处发布!
【讨论】:
【参考方案2】:我刚刚遇到了类似的问题,因为我自己的 UITapGestureRecognizer
与 MKMapView
的选择手势识别器竞争。我在点击事件上创建并选择了注释,但是因为两个手势识别器都在触发,而它们的最后触发,所以它立即取消选择。
我的解决方案是实现UIGestureRecognizerDelegate
并成为我的手势识别器的代表,并实现以下内容:
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool
return true
【讨论】:
以上是关于添加注释后选择带有自定义标注的 MKMarkerAnnotationView的主要内容,如果未能解决你的问题,请参考以下文章