gpt4 book ai didi

ios - 当用户使用 MapKit、Swift 点击 map 时如何执行操作

转载 作者:行者123 更新时间:2023-11-29 01:19:39 26 4
gpt4 key购买 nike

我有自定义 View ,当用户点击注释时会出现这些 View ,但我不知道当用户点击 map 上的其他位置时如何使这些 View 消失。在适用于 iOS 的 Google Map SDK 中,它将是 didTapAtCooperative,然后我将设置 view.hidden = true

我认为可以使用touchBegan来完成,尽管我以前没有使用过这个功能?虽然我更喜欢 Google 的 MapKit 版本 didTapAtCooperative

编辑

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,则自定义 View 永远不会出现。

最佳答案

我认为 Igor 的回答 https://stackoverflow.com/a/11455213/4205432对你有好处,我只是把它翻译成 swift。你有一个委托(delegate)方法,你可以用它来知道哪个注释 View 被取消选择并在那里做你的事情。 正如 Igor 的回答中提到的,您可以添加一个 bool 标志来标识是否为“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
}

关于ios - 当用户使用 MapKit、Swift 点击 map 时如何执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768303/

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