向下或向上滑动MKMapView指定的像素数

时间:2017-03-08 13:50:00

标签: ios swift mkmapview

我如何垂直给定数量的像素滑动MKMapView,我玩了一下,这是我的代码,其中的幕后不起作用:(

var point = mapView.convert(mapView.centerCoordinate, toPointTo: self.view)

point.x += offSet.x
point.y += offSet.y

let center = mapView.convert(point, toCoordinateFrom: self.view)
mapView.setCenter(center, animated: true)

1 个答案:

答案 0 :(得分:1)

刚想出来,这是代码,希望它会帮助别人;)

func mapViewMoveBy(offset: CGPoint, animated: Bool = true) {
    var point = mapView.center

    point.x += offset.x
    point.y += offset.y

    let coordinate = mapView.convert(point, toCoordinateFrom: mapView)
    mapView.setCenter(coordinate, animated: animated)
}

<强>用法

let slideFourtyPixelDown: CGFloat = -40
mapViewMoveBy(offset: CGPoint(x: 0, y: slideFourtyPixelDown))