Gestures

As you may have seen from the Get Started example, a map view by default supports all common map gestures, for example, pinch and double tap to zoom in. The following table is a summary of the available gestures and their corresponding default behavior on the map.

 Tap the screen with one finger. This gesture does not have a predefined map action. To zoom the map in by a fixed amount, tap the screen twice with one finger. Press and hold one finger to the screen. This gesture does not have a predefined map action. To move the map, press and hold one finger to the screen, and move it in any direction. The map will keep moving with a little momentum after the finger was lifted. To tilt the map, press and hold two fingers to the screen, and move them vertically. No behavior is predefined for other directions. To zoom out by a fixed amount, tap the screen with two fingers. To zoom in or out continuously, press and hold two fingers to the screen, and increase or decrease the distance between them. To rotate the map continuously, press and hold two fingers to the screen, and change the angle between them either by rotating them both or by moving one of them.

The HERE SDK for iOS provides support for the following gestures:

• Tap: TapDelegate
• Double Tap: DoubleTapDelegate
• Long Press: LongPressDelegate
• Pan: PanDelegate
• Two Finger Pan: TwoFingerPanDelegate
• Two Finger Tap: TwoFingerTapDelegate
• Pinch Rotate: PinchRotateDelegate

Each delegate provides a dedicated callback that informs you whenever the user performs an action that could be detected, for example, the beginning or the end of that specific gesture. Usually, you want to add a specific behavior to your application after a gesture was detected, like placing a map marker after a long press.

Note that only one delegate can be set at a time for the same gesture.

Control Map Actions

Setting a delegate does not affect the default map behavior of the gestures. That can be controlled independently. By default, all standard behaviors, such as zooming in when double tapping the map, are enabled.

For example, you can disable the default map gesture behavior for double tap (zooms in) and two finger tap (zooms out) as follows:

mapView.gestures.disableDefaultAction(forGesture: .doubleTap)
mapView.gestures.disableDefaultAction(forGesture: .twoFingerTap)


When disabling a default map action, you can still listen for the gesture event. This can be useful when you want to turn off the default action of a gesture to implement your own zooming behavior, for example. All gestures - except for tap and long press - provide a default map action. More details can be found in the overview above.

To bring back the default map gesture behavior, you can call:

mapView.gestures.enableDefaultAction(forGesture: .doubleTap)
mapView.gestures.enableDefaultAction(forGesture: .twoFingerTap)


Attach a Gesture Delegate

Let's see an example of how a gesture delegate can be attached to the map view. As soon as you set a delegate, it will receive all related events for that gesture via the dedicated callback, which is onTap() in case of a TapDelegate. The class conforming to this protocol will act as the delegate:

// Conform to the TapDelegate protocol.
func onTap(origin: Point2D) {
let geoCoordinates = mapView.viewToGeoCoordinates(viewCoordinates: origin)
print("Tap at: \(String(describing: geoCoordinates))")
}


Finally, let the map view know that your class wants to get notified about the tap touch event and start listening:

mapView.gestures.tapDelegate = self


As soon as you set a delegate, you will begin to receive notifications that gestures have will been detected.

The touchPoint specifies the MapView coordinates where the gesture occurred. By calling mapView.viewToGeoCoordinates(viewCoordinates: origin), you can convert the pixels into geographic coordinates (as shown above).

Likewise, to stop listening, we can simply call:

mapView.gestures.tapDelegate = nil


For continuous gestures (like long press, pinch, pan, two finger pan), the begin gesture state will indicate that the gesture was detected. While the finger(s) still touch the display, you may receive update states, until the end state indicates that a finger has been lifted or the cancel state indicates that the gesture detection has been cancelled:

// Conform to the LongPressDelegate protocol.
func onLongPress(state: GestureState, origin: Point2D) {
if (state == .begin) {
let geoCoordinates = mapView.viewToGeoCoordinates(viewCoordinates: origin)
print("LongPress detected at: \(String(describing: geoCoordinates))")
}

if (state == .update) {
let geoCoordinates = mapView.viewToGeoCoordinates(viewCoordinates: origin)
print("LongPress update at: \(String(describing: geoCoordinates))")
}

if (state == .end) {
let geoCoordinates = mapView.viewToGeoCoordinates(viewCoordinates: origin)
print("LongPress finger lifted at: \(String(describing: geoCoordinates))")
}

if (state == .cancel) {
print("Map view lost focus. Maybe a modal dialog is shown or the app is sent to background.")
}
}


For example, a user may still keep his finger on the screen after a long press event was detected - or even move it around. However, only the begin event will mark the point in time, when the long press gesture was detected.

A long press gesture can be useful to place a map marker onto the map. An example of this can be seen in the Search example app.

Note that for the non-continuous gestures (like tap, double tap, two finger tap), no GestureState is needed to handle the gesture.

Tutorial - Customize Map Actions

As we have already seen, by default, a double tap gesture zooms in the map in discrete steps - for example, from city level closer to street level. You can disable such default map gesture actions to implement your own behaviors - or you can add your desired actions to existing behaviors.

If needed, it is also possible to combine platform gesture handling with the HERE SDK gesture detection. The HERE SDK does not provide all kinds of low level gesture events, as it focuses primarily on the common map gestures - for your convenience. If you need more granular control, you can always combine the gesture handling available from the HERE SDK with the native gesture detection.

For this tutorial, we want to show how to enable custom zoom animations: The map should zoom gradually in or out after the user has performed a double tap or a two finger tap gesture.

Let's start with the animation. For this we can use Apple's CADisplayLink to synchronize our animations with the refresh rate of the display.

For convenience, we create a new class called GestureMapAnimator, that should handle all gesture related animations. It requires a reference to the map's MapCamera instance, as the map needs to be zoomed via the camera.

Inside the GestureMapAnimator we hold a reference to our zoom animation. The CADisplayLink instance can be initialized lazily. Note that we also declare the corresponding loop method animatorLoopZoom (which we show later):

// A run loop to zoom in/out the map continuously until zoomVelocity is zero.
selector: #selector(animatorLoopZoom))


Now we can already implement a stopAnimations() method which simply pauses all ongoing animations of the corresponding CADisplayLink instance:

// Stop any ongoing zoom animation.
func stopAnimations() {
}


By default, the map zooms in/out in one discrete step at the location where the finger touches the map - without intermediate steps.

Let's hook up the needed gesture events:

mapView.gestures.disableDefaultAction(forGesture: .doubleTap)
mapView.gestures.disableDefaultAction(forGesture: .twoFingerTap)

// ...

// Conform to the DoubleTapDelegate protocol.
func onDoubleTap(origin: Point2D) {
// Start our custom zoom in animation.
gestureMapAnimator.zoomIn(origin)
}

// Conform to the TwoFingerTapDelegate protocol.
func onTwoFingerTap(origin: Point2D) {
// Start our custom zoom out animation.
gestureMapAnimator.zoomOut(origin)
}


No magic here: We simply listen for the two gesture events. We just need to make sure that the default zoom behavior is disabled in advance. The zoomIn() and zoomOut() methods from above lead to a new method in our GestureMapAnimator that you can see below.

Additionally, we have to define two constants to determine the starting value startZoomVelocity for our zoom animation and the amount zoomDelta by which the current zoomVelocity value should decrease over time. The startZoomAnimation() method takes care to start the run loop:

private let startZoomVelocity: Double = 0.1
private let zoomDelta: Double = 0.005

// Starts the zoom in animation.
func zoomIn(_ origin: Point2D) {
zoomOrigin = origin
isZoomIn = true
startZoomAnimation()
}

// Starts the zoom out animation.
func zoomOut(_ origin: Point2D) {
zoomOrigin = origin
isZoomIn = false
startZoomAnimation()
}

private func startZoomAnimation() {
stopAnimations()

zoomVelocity = startZoomVelocity
}


We store the zoomOrigin to know where on the map we should zoom in or out.

Note that we use the flag isZoomIn to be able to handle the zooming code in one method. To start the animation we unpause the displayLinkZoom loop and update the zoom values in the corresponding selector method animatorLoopZoom(). This method will be called periodically until displayLinkZoom is paused again by calling the stopAnimations() method we have already seen before:

@objc private func animatorLoopZoom() {
var zoomFactor: Double = 1
zoomFactor = isZoomIn ? zoomFactor + zoomVelocity : zoomFactor - zoomVelocity;
// zoomFactor values > 1 will zoom in and values < 1 will zoom out.
camera.zoomBy(zoomFactor, around: zoomOrigin)
zoomVelocity = zoomVelocity - zoomDelta
if (zoomVelocity <= 0) {
stopAnimations()
}
}


The simple algorithm above interpolates the zoomVelocity value from 0.1 close to 0. zoomVelocity is the animated value we can use to gradually zoom the map. Our starting value is defined in startZoomVelocity as 0.1. We use zoomVelocity as argument to set the zoom factor. As zoomVelocity slowly reaches 0, the resulting zoom step will get smaller and smaller.

Above, we use the touch origin that we stored in zoomOrigin to zoom in to the point where the finger has touched the screen. For zooming out, this will be the point between the two finger tap gesture. The camera's zoomBy() method then takes care to perform the discrete zoom step at the specified location.

The boolean flag isZoomIn indicates if the map should be zoomed in or out. This enables us to use the code for both zoom cases: The only difference is that the animated value zoomVelocity is added or subtracted from the current zoom factor.

The zoom factor specifies how far the map is zoomed in or out. A zoom factor of 1 does not change the current zoom level.

Now, that's it for our little excursion. Feel free to adapt the above code snippets to your own needs. For example, start by playing around with different interpolators, different animated values, or different animation durations.