Используя ответ Марко, я пришел к этому решению.
Каждый раз, когда меняется регион, я меняю свойство ViewController's
isAtBigZoom
.
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
isAtBigZoom = mapView.region.span.latitudeDelta < 0.01
}
Затем на didSet
свойства я выполняю этот код.
var isAtBigZoom = false {
didSet {
// this guard ensures, that the showing and hiding happens only once
guard oldValue != isAtBigZoom else {
return
}
// in my case I wanted to show/hide only a certain type of annotations
for case let annot as MapTextAnnotation in mapView.annotations {
mapView.viewForAnnotation(annot)?.alpha = isAtBigZoom ? 1 : 0
}
}
}
Если вы также хотите начать со скрытых аннотаций, просто добавьте код изменения альфы в метод viewForAnnotation
.
Отлично работает, и я не заметил больших проблем с производительностью. Хотя это может измениться с увеличением количества аннотаций ...