Skip to content

Commit

Permalink
Option to disable resizing animation (#2374)
Browse files Browse the repository at this point in the history
Co-authored-by: Mai Mai <[email protected]>
  • Loading branch information
OdNairy and maios authored Dec 3, 2024
1 parent 997c751 commit 5c9ce70
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Apps/Examples/Examples/All Examples/Lab/ResizeMapViewExample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ final class ResizeMapViewExample: UIViewController, ExampleProtocol {

setupDefaultCamera()
applyAutolayout()

let animationBehaviourButton = UIBarButtonItem(
title: nil,
style: .done,
target: self,
action: #selector(toggleAnimationBehaviour(_:))
)
navigationItem.rightBarButtonItem = animationBehaviourButton
syncAnimationBehaviourButton(animationBehaviourButton)
}

func mapStyleDidLoad() {
Expand Down Expand Up @@ -128,6 +137,39 @@ final class ResizeMapViewExample: UIViewController, ExampleProtocol {
lastManualResizingDirection.toggle()
}

var animationBehaviour: MapView.ResizingAnimation = .automatic {
didSet {
mapboxMapView.resizingAnimation = animationBehaviour
}
}

@objc
private func toggleAnimationBehaviour(_ sender: UIBarButtonItem) {
switch animationBehaviour {
case .automatic:
animationBehaviour = .none
case .none:
animationBehaviour = .automatic
}

syncAnimationBehaviourButton(sender)
}

private func syncAnimationBehaviourButton(_ button: UIBarButtonItem) {
switch animationBehaviour {
case .automatic:
if #available(iOS 13.0, *) {
button.image = UIImage(systemName: "arrow.up.arrow.down.circle.fill")
}
button.title = "Automatic"
case .none:
if #available(iOS 13.0, *) {
button.image = UIImage(systemName: "arrow.up.arrow.down.circle")
}
button.title = "None"
}
}

// MARK: -

func setupDefaultCamera() {
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Mapbox welcomes participation and contributions from everyone.

## main

* Add a new API to disable custom resizing implementation of the MapView. To disable the custom resizing implementation, set `MapView.resizingAnimation` to `.none`.

## 11.9.0-beta.1

⚠️⚠️⚠️ Potentially breaking changes ⚠️⚠️⚠️
Expand Down
48 changes: 48 additions & 0 deletions Sources/MapboxMaps/Foundation/MapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,52 @@ open class MapView: UIView, SizeTrackingLayerDelegate {

metalView?.center = CGPoint(x: bounds.midX, y: bounds.midY)
safeAreaSignalSubject.value = self.safeAreaInsets

if let metalView, metalView.autoResizeDrawable {
metalView.frame = bounds
mapboxMap.size = metalView.bounds.size
}
}

#if !os(visionOS)
/// Control the resizing animation behavior of the map view.
/// The default value is ``ResizingAnimation-swift.enum/automatic``.
public enum ResizingAnimation {
/// Change the default behaviour to have a nice looking resizing animation.
///
/// The map plane would fulfil the MapView sized all the time.
/// Custom implementation.
case automatic

/// Default UIView behaviour. The map plane would be resized immediately leading to gaps renderer when assign higher size values.
case none

init?(autoResizeDrawable: Bool?) {
guard let autoResizeDrawable else { return nil }
self = autoResizeDrawable ? .none : .automatic
}

var autoResizeDrawable: Bool {
switch self {
case .automatic: return false
case .none: return true
}
}
}

/// Control resizing animation behavior of the map view.
public var resizingAnimation: ResizingAnimation = .automatic {
didSet {
syncResizingAnimation()
}
}

private func syncResizingAnimation() {
// VisionOS doesn't support autoResizeDrawable
metalView?.autoResizeDrawable = resizingAnimation.autoResizeDrawable
}
#endif

/// Synchronize size updates with GL-Native and UIKit
///
/// To provide nice custom resizing behavior SDK rely on custom `drawableSize` updates
Expand Down Expand Up @@ -740,6 +784,10 @@ extension MapView: DelegatingMapClientDelegate {

self.metalView = metalView

#if !os(visionOS)
syncResizingAnimation()
#endif

return metalView
}
}
Expand Down

0 comments on commit 5c9ce70

Please sign in to comment.