Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created May 11, 2017 20:37
Show Gist options
  • Select an option

  • Save DonMag/dd415541949d7806bb182a260a55d4dd to your computer and use it in GitHub Desktop.

Select an option

Save DonMag/dd415541949d7806bb182a260a55d4dd to your computer and use it in GitHub Desktop.
var startPoint = CGPoint.zero
var startCoord = CLLocationCoordinate2D(latitude: 48.8582, longitude: 2.2945)
@IBAction func didPan(_ sender: Any) {
if let recognizer = sender as? UIPanGestureRecognizer {
// translate touch to local view
let trans = recognizer.translation(in: recognizer.view)
switch recognizer.state {
// when PAN begins, save the firstTouchPoint in the touched view, and save the current map center
case .began:
startPoint = trans
startCoord = mapView.centerCoordinate
break
// when PAN moves, get the distance from the start touch point, add that to the latitude of the saved coord, and set the new map region
case .changed:
let delta = startPoint.x - trans.x
let newCoord = CLLocationCoordinate2D(latitude: startCoord.latitude + Double(delta / 1000), longitude: startCoord.longitude)
let region = MKCoordinateRegionMakeWithDistance(newCoord, 0.1, 0.1)
self.mapView.setRegion(region, animated: false)
break
default:
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment