Skip to content

Instantly share code, notes, and snippets.

@boundsj
Last active August 4, 2016 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boundsj/d299a20ac6393c298e08cdc9cb75d2d9 to your computer and use it in GitHub Desktop.
Save boundsj/d299a20ac6393c298e08cdc9cb75d2d9 to your computer and use it in GitHub Desktop.
Force touch annotations

Since an MGLAnnotationView is a UIView you can use familiar APIs to manipulate it. For example, on devices that support 3D touch, you can transform the view as a function of the touch force.

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    super.touchesMoved(touches, withEvent: event)

    if let touch = touches.first {
        let normalizedForce = touch.force / touch.maximumPossibleForce * 2.5
        self.transform = CGAffineTransformMakeScale(1 + normalizedForce, 1 + normalizedForce)
    }
}

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    super.touchesCancelled(touches, withEvent: event)

    UIView.animateWithDuration(0.25) {
        self.transform = CGAffineTransformMakeScale(1, 1)
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    super.touchesEnded(touches, withEvent: event)

    UIView.animateWithDuration(0.25) {
        self.transform = CGAffineTransformMakeScale(1, 1)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment