mapkit 缩小然后放大

mapkit zoom out and then zoom in

我有一张放大的地图。当用户选择一个新的 poi 时,我想缩小(动画)并在动画后放大到新的 poi。

但是,它只是缩小,而不是缩小。如果我在缩小时使用 animated:false,它就起作用了。

地图缩小动画完成后如何放大?

  func centerMapOnLocation(location: CLLocation) {

    //Är kartan inzoomad.. zooma ut först.
    if isZoomed
    {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
            20000, 20000)
        OverviewMap.setRegion(coordinateRegion, animated: false)

    }

    let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
        regionRadius * 4.0, regionRadius * 4.0)
    OverviewMap.setRegion(coordinateRegion, animated: true)
    isZoomed=true
}

您需要确保在调用下一个 setRegion 之前完成一个 setRegion 动画。

查看 MKmapViewDelegate regionDidChangeAnimated 方法。这将允许您对 setRegion 动画的完成做出反应并链接下一个 setRegion 动画。

这段代码应该可以满足您的需求。 didSelect AnnotationView 在用户点击图钉时触发。

var zoomingIn = false
var zoomingAnnotation:MKAnnotation

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView)
{
    let pin = view as! MKPinAnnotationView
    zoomInOnPin(pin.annotation!)
}

func zoomInOnPin(annotation:MKAnnotation) {
    let zoomOutRegion = MKCoordinateRegion(center: mapView.region.center, span: MKCoordinateSpan(latitudeDelta: 0.09, longitudeDelta: 0.09))
    zoomingIn = true
    zoomingAnnotation = annotation
    mapView.setRegion(zoomOutRegion, animated: true)
}

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
    if let annotation = zoomingAnnotation where zoomingIn == true {
        zoomingIn = false
        let region = MKCoordinateRegion(center: zoomingAnnotation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.07, longitudeDelta: 0.07))
        mapView.setRegion(region, animated: true)
    }
}