How to define the order of overlapping MKAnnotationViews?

后端 未结 7 1397
谎友^
谎友^ 2020-11-30 06:29

I have several MKAnnotations (and their corresponding views) in my map, and it sometimes gets really crowded. Now, the annotations in my app come in two flavors: some are bo

相关标签:
7条回答
  • 2020-11-30 07:16

    Swift 3:

    I get pin locations from API and I was having similar issues, the pins that had to be on top weren't. I was able to solve it like this.

    var count = 0 // just so we don't get the same index in bottom pins
    func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {  
        for view in views {
            view.layer.zPosition = CGFloat(count)
        }
        count += 1
        if count > 500 {
            count = 250 // just so we don't end up with 999999999999+ as a value for count, plus I have at least 30 pins that show at the same time and need to have lower Z-Index values than the top pins. 
        }
    
    }
    

    Hope this helps

    0 讨论(0)
提交回复
热议问题