覆盖 MKMapView scroll/pan 性能很差

Overriding MKMapView scroll/pan has very poor performance

我想将 MKMapView 的平移限制为仅 horizontal/longitude 滚动。

我将要保留的纬度设置在一个变量中:

var currentLatitude: CLLocationDegrees?

然后我覆盖 mapViewDidChangeVisibleRegion 委托方法以将地图重置为纬度(如果它已更改)。它可以工作,但是平移性能很糟糕。

    func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
        
        let coordinates = CLLocationCoordinate2D(latitude: currentLatitude ?? mapView.region.center.latitude, longitude: mapView.region.center.longitude)
        let newRegion = MKCoordinateRegion(center: coordinates, span: mapView.region.span)
        mapView.setRegion(newRegion, animated: false)
    }

我尝试在 regionDidChangeAnimated 中使用相同的代码,但它可以工作,但它允许垂直平移,然后当用户手指抬起时,它会跳回正确的纬度。

还有 regionWillChangeAnimated,但我不认为这会在更改之前为您提供新坐标。

尝试为 MKMapView 设置 CameraBoundary,纬度跨度为零,经度跨度最大,例如:

let coordinateRegion =  MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.5072, longitude: -0.1276),
                                           latitudinalMeters: 0,
                                           longitudinalMeters: .greatestFiniteMagnitude)

let boundary = MKMapView.CameraBoundary(coordinateRegion: coordinateRegion)

mapView.setCameraBoundary(boundary, animated: false)