WhirlyGlobe icon indicating copy to clipboard operation
WhirlyGlobe copied to clipboard

[iOS]How can I stop moving the map immediately after I stop gesturing?

Open FeiXueKK opened this issue 5 years ago • 2 comments

RT

FeiXueKK avatar May 17 '19 08:05 FeiXueKK

I fixed this by:

  • adding a UIPanGestureRecognizer to my WhirlyGlobeViewController's view
  • adding a MaplyCoordinate var that is used to keep track of where the user's gesture ends - I called this forcedCenter
  • the action of the UIPanGestureRecogniser checks the velocity when the gesture ends, and if it's less than 50px/second it sets forcedCenter. Code if it helps:
@IBAction func didPan(_ sender: Any) {
    if self.panGestureRecognizer.state == .ended {
        let velocity = self.panGestureRecognizer.velocity(in: self.globe.view)
        guard abs(velocity.x) < 50 && abs(velocity.y) < 50 else { return }
        self.forcedCenter = self.globe.getPosition()
    }
}
  • globeViewController(_:, didMove:) keeps firing, but it's suppressed like so:
func globeViewController(_ viewC: WhirlyGlobeViewController, didMove corners: UnsafeMutablePointer<MaplyCoordinate>) {
    if let forcedCenter = self.forcedCenter {
        viewC.setPosition(forcedCenter)
    }
}
  • finally, clear out forcedCenter when the globe eventually stops moving:
func globeViewController(_ viewC: WhirlyGlobeViewController, didStopMoving corners: UnsafeMutablePointer<MaplyCoordinate>, userMotion: Bool) {
    self.forcedCenter = nil
}

majsty avatar Jul 10 '19 12:07 majsty

Nice!

mousebird avatar Jul 10 '19 17:07 mousebird