我有自定义视图,当用户点击注释时会出现,但我不知道当用户点击地图上的其他地方时,这些视图会如何消失。
在Google Map SDK for iOS中,它将是didTapAtCoordinate
,然后我会设置view.hidden = true
。
我认为可以使用touchBegan
完成,虽然我之前没有使用过这个功能?虽然我更喜欢MapKit版本的谷歌didTapAtCoordinate
。
修改
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var myCustomView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
myCustomView.hidden = true
//MyAnnatationClass is my class where I store MKAnnatations with additional details
let ann = MyAnnatationClass(coordinate: CLLocationCoordinate2D(latitude: 54.694084, longitude: 25.288782), title: "DEMO2", name: "Some Name")
mapView.addAnnotation(ann)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "id"
if annotation.isKindOfClass(MyAnnatationClass.self) {
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.image = UIImage(named: "pin.png")
annotationView!.canShowCallout = true
} else {
annotationView!.annotation = annotation
}
return annotationView
}
return nil
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
//Set view details
let myAnn = view.annotation as! MyAnnatationClass
someViewLabel.text = myAnn.name
myCustomView.hidden = false
let annotationTap = UITapGestureRecognizer(target: self, action: "tapRecognized:")
annotationTap.numberOfTapsRequired = 1
view.addGestureRecognizer(annotationTap)
let selectedAnnotations = mapView.selectedAnnotations
for annotationView in selectedAnnotations{
mapView.deselectAnnotation(annotationView, animated: true)
}
}
func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
view.hidden = true
}
func tapRecognized(gesture:UITapGestureRecognizer){
let selectedAnnotations = mapView.selectedAnnotations
for annotationView in selectedAnnotations{
mapView.deselectAnnotation(annotationView, animated: true)
}
}
}
目前,当我触摸注释时,会出现myCustomView
并且注释会消失。如果在didDeselectAnnotationView
我将view
更改为myCustomView
,则自定义视图永远不会显示。
答案 0 :(得分:0)
我认为伊戈尔的回答https://stackoverflow.com/a/11455213/4205432对你有好处,我只是把它翻译成了一个快速的。你有一个委托方法,你可以用它来知道取消选择哪个注释视图并在那里做你的东西。 正如在Igor的回答中所提到的,你可以添加一个布尔标志来识别是否' isShow'或不。
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
let annotationTap = UITapGestureRecognizer(target: self, action: "tapRecognized")
annotationTap.numberOfTapsRequired = 1
view.addGestureRecognizer(annotationTap)
let selectedAnnotations = mapView.selectedAnnotations
for annotationView in selectedAnnotations{
mapView.deselectAnnotation(annotationView, animated: true)
}
}
func tapRecognized(gesture:UITapGestureRecognizer){
let selectedAnnotations = mapView.selectedAnnotations
for annotationView in selectedAnnotations{
mapView.deselectAnnotation(annotationView, animated: true)
}
}
func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) {
view.hidden = true
}
答案 1 :(得分:0)
我建议使用内置的注释标注机制。您可以创建自定义标注视图,系统会显示该视图以响应用户点击注释,如果用户点击其他位置,系统也会将其设置为动画。
地图视图已广泛使用手势,因此尝试在顶部添加自己的手势处理将会很困难。