ViewportState
public protocol ViewportState : AnyObject
ViewportState is a protocol that Viewport depends on as it orchestrates transitions to and
from different states.
A ViewportState is a reference type and must not be shared among multiple Viewport
instances simultaneously.
The observeDataSource(with:) method allows
ViewportTransitions to consume a stream of camera updates from a target state while
executing a transition. startUpdatingCamera() and
stopUpdatingCamera() are invoked to tell the state that it should assume or
relinquish control of the map’s camera. These are typically used by Viewport itself after a
successful transition into a state and when exiting a state, respectively.
MapboxMaps provides implementations of ViewportState that can be created and configured
via methods on Viewport. Applications may also define their own implementations to handle
advanced use cases not covered by the provided implementations.
States should generally pre-warm their data sources as soon as they are created to minimize delays when they become current. For this reason, only states that are currently (or soon-to-be) needed should be kept alive so that unneeded resources (e.g. location services) can be released.
-
Registers a
handlerto receive the cameras being generated by thisViewportState.This method is commonly used by
ViewportTransitionimplementations to obtain the target camera for transition animations. Transitions typically cannot start their animations until afterhandleris invoked for the first time, so it’s a good idea for states to invokehandlerwith the current camera if it’s not too stale rather than waiting for the next camera change to occur. To increase the likelihood that a valid camera exists when a handler is registered, designViewportStateimplementations so that they start updating their internal state prior to when they are passed totransition(to:transition:completion:).The caller may either cancel the returned
Cancelableor returnfalsefromhandlerto indicate that it wishes to stop receiving updates. Following either of these events, implemenations must no longer invokehandlerand must release all strong references to it.Declaration
Swift
func observeDataSource(with handler: @escaping (_ camera: CameraOptions) -> Bool) -> CancelableParameters
handlerA closure that is invoked by the state whenever its camera changes. Returns
trueto stay subscribed andfalseto unsubscribe.handlermust be invoked on the main queue.cameraThe
ViewportState‘s most recent camera.Return Value
A
Cancelablethat the caller can use to unsubscribe. -
Tells this state that it is now responsible for updating the camera.
Viewportcalls this method at the end of a successful transition into this state.Implementations typically have a dependency on either
MapboxMapso that they can use itssetCamera(to:)method to change the camea or onCameraAnimationsManagerso that they can run camera animations.Declaration
Swift
func startUpdatingCamera() -
Tells this state that it is no longer responsible for updating the camera.
Viewportcalls this method at the beginning of the transition out of this state.Implementations must stop updating the camera immediately and should typically cancel any ongoing animations that they started when this method is invoked.
Declaration
Swift
func stopUpdatingCamera()
ViewportState Protocol Reference