Search code examples
iosswiftmkmapview

Slide MKMapView specified number of pixels down or up


How would I slide MKMapView by given number of pixels vertically, I played it around a bit, here is my code, which offcourse does not work :(

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)

Solution

  • Just figured it out, here is the code, hope it will help someone ;)

    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)
    }
    

    Usage

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