didSelect为MKAnnotationView没有射击
问题描述:
我有麻烦可以在MapView中选择自定义注释。我尝试几种方法,都没有成功:didSelect为MKAnnotationView没有射击
- 使用MKMapViewDelegate的didSelect
- 使用的UIButton被添加到AnnotationView
- 使用UITapGestureRecognizer的ImageView的,它应该用于注释
你可以找到所有这些方法在下面的代码注释:
override func viewDidLoad() {
super.viewDidLoad()
// some more initialization code
for item in items {
let annotation = IdentifiedMKPointAnnotation() // has only one additional property named "id"
annotation.coordinate = CLLocationCoordinate2D(latitude: item.spots[0].latitude!, longitude: item.spots[0].longitude!)
annotation.id = i
self.mapView.addAnnotation(annotation)
i += 1
}
}
// MARK: - MapViewDelegate
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let item = self.items[(annotation as! IdentifiedMKPointAnnotation).id!]
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "")
//annotationView.canShowCallout = false
//annotationView.isEnabled = false
let imageView = UIImageView()
imageView.frame.size = CGSize(width: 40, height: 40)
imageView.layer.cornerRadius = 20
imageView.layer.masksToBounds = true
imageView.af_setImage(withURL: URL(string: item.images.count > 0 ? item.images[0] : item.image!)!)
imageView.contentMode = .scaleAspectFill
//let gr = UIGestureRecognizer(target: self, action: #selector(annotationSelected(_:)))
//imageView.addGestureRecognizer(gr)
//imageView.isUserInteractionEnabled = true
//let button = UIButton()
//button.frame.size = CGSize(width: 40, height: 40)
//button.setImage(#imageLiteral(resourceName: "play_arrow"), for: .normal)
//button.addTarget(self, action: #selector(annotationSelected(_:)), for: .touchUpInside)
annotationView.addSubview(imageView)
//annotationView.addSubview(button)
return annotationView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
performSegue(withIdentifier: "showDetail", sender: self.items[(view.annotation as! IdentifiedMKPointAnnotation).id!])
view.setSelected(false, animated: false)
}
func annotationSelected(_ sender: UIImageView) {
performSegue(withIdentifier: "showDetail", sender: self.items[((sender.superview as! MKAnnotationView).annotation as! IdentifiedMKPointAnnotation).id!])
}
任何想法为什么这个动作不是/不是正在射击?
答
刚刚发现基于以下职位的解决方案:https://*.com/a/28671680/6010489
这足以设置annotationview的高度和宽度:
annotationView.frame.size.height = 40
annotationView.frame.size.width = 40
答
如果注释的title
为空,则不调用didSelect
委托。尝试下面的代码。
let annotation = IdentifiedMKPointAnnotation()
annotation.title = "title"
+0
以外的一点点我已经尝试设置标题 - 仍然无法正常工作。 –
我没有看到任何地方设置委托在码。你真的做到了吗? –
是的,我已经在故事板中做过了。奇怪的是,那“didSelect”的作品,当我点击圆形imageview –