Select 添加注释后带有自定义标注的 MKMarkerAnnotationView
Select MKMarkerAnnotationView with custom callout after adding annotation
我有一个 MKMapView,我想在其上添加长按手势的注释。添加注释后,我想 select 注释视图。我认为这是一个非常简单的请求。
问题是我正在使用带有自定义 rightCalloutAccessoryView 和自定义 detailCalloutAccessoryView 的新 MKMarkerAnnotationView。目前还没有很好的记录,但 WWDC 2017 Session 237 声明当超过 title/subtitle.
时将显示标注
不幸的是,我不是这种情况。当我以编程方式(和手动)select 注释时,我得到一个奇怪的双重 selected 状态,我可以在其中看到标注和标记:
这里是注解查看代码:
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:)
方法,但结果更糟,因为标注前面的标记视图。
非常感谢任何帮助!谢谢!
我找到了以下解决方法:添加注释时,请稍等片刻,然后再以编程方式选择它:
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)
}
}
我仍然认为这应该不是必需的,所以如果您有更好的解决方案,请随时post在这里!
我刚刚遇到了类似的问题,因为我有一个自己的 UITapGestureRecognizer
与 MKMapView
的选择手势识别器竞争。我在点击事件上创建并选择了注释,但是因为两个手势识别器都在触发,而它们最后触发,所以它立即取消选择。
我的解决方案是实施 UIGestureRecognizerDelegate
并成为我的手势识别器的代表,并实施以下内容:
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
我有一个 MKMapView,我想在其上添加长按手势的注释。添加注释后,我想 select 注释视图。我认为这是一个非常简单的请求。
问题是我正在使用带有自定义 rightCalloutAccessoryView 和自定义 detailCalloutAccessoryView 的新 MKMarkerAnnotationView。目前还没有很好的记录,但 WWDC 2017 Session 237 声明当超过 title/subtitle.
时将显示标注不幸的是,我不是这种情况。当我以编程方式(和手动)select 注释时,我得到一个奇怪的双重 selected 状态,我可以在其中看到标注和标记:
这里是注解查看代码:
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:)
方法,但结果更糟,因为标注前面的标记视图。
非常感谢任何帮助!谢谢!
我找到了以下解决方法:添加注释时,请稍等片刻,然后再以编程方式选择它:
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)
}
}
我仍然认为这应该不是必需的,所以如果您有更好的解决方案,请随时post在这里!
我刚刚遇到了类似的问题,因为我有一个自己的 UITapGestureRecognizer
与 MKMapView
的选择手势识别器竞争。我在点击事件上创建并选择了注释,但是因为两个手势识别器都在触发,而它们最后触发,所以它立即取消选择。
我的解决方案是实施 UIGestureRecognizerDelegate
并成为我的手势识别器的代表,并实施以下内容:
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}