From 1ea0913a2c0e28cc7627c98645eefedad57e251b Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Fri, 12 Feb 2021 14:26:11 -0500 Subject: [PATCH 01/12] code clean up --- .../GestureHandlers/PanGestureHandler.swift | 11 ++++------- Mapbox/MapboxMapsGestures/GestureManager.swift | 5 +---- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift b/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift index accfb994ff30..f2e23eacd6e7 100644 --- a/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift +++ b/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift @@ -29,21 +29,18 @@ internal class PanGestureHandler: GestureHandler { pan.setTranslation(.zero, in: pan.view) case .ended, .cancelled: var velocity = pan.velocity(in: pan.view) - if self.decelerationRate == 0.0 - || (sqrt(pow(velocity.x, 2) + pow(velocity.y, 2)) < 100) { - // Not enough velocity to overcome friction - velocity = CGPoint.zero + if self.decelerationRate == 0.0 || (sqrt(pow(velocity.x, 2) + pow(velocity.y, 2)) < 100) { + velocity = CGPoint.zero // Not enough velocity to overcome friction } if velocity != CGPoint.zero { // There is a potential drift after the gesture has ended - let offset = CGPoint(x: velocity.x * decelerationRate / 4, - y: velocity.y * decelerationRate / 4) + let offset = CGPoint(x: velocity.x * decelerationRate, + y: velocity.y * decelerationRate) .applyPanScrollingMode(panScrollingMode: scrollMode) self.delegate.panEnded(with: offset) } - default: break } diff --git a/Mapbox/MapboxMapsGestures/GestureManager.swift b/Mapbox/MapboxMapsGestures/GestureManager.swift index 2f52bfd508df..712b3c5c12b8 100644 --- a/Mapbox/MapboxMapsGestures/GestureManager.swift +++ b/Mapbox/MapboxMapsGestures/GestureManager.swift @@ -283,10 +283,7 @@ extension GestureManager: GestureHandlerDelegate { // Pan has ended on the MapView with a residual `offset` internal func panEnded(with offset: CGPoint) { - if let pitch = self.cameraManager.mapView?.pitch, - pitch == 0.0 { - self.cameraManager.moveCamera(by: offset, animated: true) - } + self.cameraManager.moveCamera(by: offset, animated: true) } internal func cancelGestureTransitions() { From 969b1808dca6476fce89dbeb102fca02ac12c714 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Tue, 16 Feb 2021 13:59:43 -0500 Subject: [PATCH 02/12] fix min velocity threshold --- .../GestureHandlers/PanGestureHandler.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift b/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift index f2e23eacd6e7..672cec07e299 100644 --- a/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift +++ b/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift @@ -29,9 +29,11 @@ internal class PanGestureHandler: GestureHandler { pan.setTranslation(.zero, in: pan.view) case .ended, .cancelled: var velocity = pan.velocity(in: pan.view) + let velocityHypot = sqrt(pow(velocity.x, 2) + pow(velocity.y, 2)) - if self.decelerationRate == 0.0 || (sqrt(pow(velocity.x, 2) + pow(velocity.y, 2)) < 100) { - velocity = CGPoint.zero // Not enough velocity to overcome friction + if self.decelerationRate == 0.0 || velocityHypot < 1000 { + velocity = CGPoint.zero + return } if velocity != CGPoint.zero { // There is a potential drift after the gesture has ended From 55c5383f50b1871d2066590062af6d5906363002 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Tue, 16 Feb 2021 14:58:00 -0500 Subject: [PATCH 03/12] adding logic for decelration --- .../GestureHandlers/PanGestureHandler.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift b/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift index 672cec07e299..debfadafe756 100644 --- a/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift +++ b/Mapbox/MapboxMapsGestures/GestureHandlers/PanGestureHandler.swift @@ -33,12 +33,11 @@ internal class PanGestureHandler: GestureHandler { if self.decelerationRate == 0.0 || velocityHypot < 1000 { velocity = CGPoint.zero - return } if velocity != CGPoint.zero { // There is a potential drift after the gesture has ended - let offset = CGPoint(x: velocity.x * decelerationRate, - y: velocity.y * decelerationRate) + let offset = CGPoint(x: velocity.x * decelerationRate / 4, + y: velocity.y * decelerationRate / 4) .applyPanScrollingMode(panScrollingMode: scrollMode) self.delegate.panEnded(with: offset) From 27ea17d228a213057f74da6d18bd0600e2055213 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Tue, 16 Feb 2021 15:29:05 -0500 Subject: [PATCH 04/12] account for fling --- .../Camera/CameraManager.swift | 18 ++++++++++++++---- Mapbox/MapboxMapsGestures/GestureManager.swift | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift b/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift index 95155b5becdd..8c80fec106fb 100644 --- a/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift +++ b/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift @@ -401,13 +401,13 @@ public class CameraManager { - Parameter zoom: The amount to adjust the camera's zoom level by. - Parameter animated: Indicates whether the camera changes should be animated. */ - public func moveCamera(by offset: CGPoint? = nil, rotation: CGFloat? = nil, pitch: CGFloat? = nil, zoom: CGFloat? = nil, animated: Bool = false) { + public func moveCamera(by offset: CGPoint? = nil, rotation: CGFloat? = nil, pitch: CGFloat? = nil, zoom: CGFloat? = nil, animated: Bool = false, didFling: Bool = false) { guard let mapView = mapView else { assertionFailure("MapView is nil.") return } - let centerCoordinate = self.shiftCenterCoordinate(by: offset ?? .zero) + let centerCoordinate = self.shiftCenterCoordinate(by: offset ?? .zero, didFling: didFling) var newBearing: CGFloat = 0 if let angle = rotation { @@ -456,7 +456,7 @@ public class CameraManager { Return a new center coordinate shifted by a given offset value. - Parameter offset: The `CGPoint` value to shift the map's center by. */ - func shiftCenterCoordinate(by offset: CGPoint) -> CLLocationCoordinate2D { + func shiftCenterCoordinate(by offset: CGPoint, didFling: Bool) -> CLLocationCoordinate2D { guard let mapView = mapView else { assertionFailure("MapView is nil.") return CLLocationCoordinate2D(latitude: 0, longitude: 0) @@ -465,13 +465,23 @@ public class CameraManager { return CLLocationCoordinate2D(latitude: 0, longitude: 0) } + var pitchFactor = mapView.pitch + if didFling { + if pitchFactor != 0.0 { + pitchFactor /= 10.0 + pitchFactor += 1.5 + } + } else { + pitchFactor = 1.0 // We do not want divide by 0 + } + let cameraViewSize = mapView.cameraView.frame.size let cameraPadding = mapView.cameraView.padding let viewPortSize = CGSize(width: cameraViewSize.width - cameraPadding.left - cameraPadding.right, height: cameraViewSize.height - cameraPadding.top - cameraPadding.bottom) let viewPortCenter = CGPoint(x: (viewPortSize.width / 2) + cameraPadding.left, y: (viewPortSize.height / 2) + cameraPadding.top) - let newViewPortCenter = CGPoint(x: viewPortCenter.x - offset.x, y: viewPortCenter.y - offset.y) + let newViewPortCenter = CGPoint(x: viewPortCenter.x - (offset.x / pitchFactor), y: viewPortCenter.y - (offset.y / pitchFactor)) var centerCoordinate = mapView.coordinate(for: newViewPortCenter) var newLong: Double diff --git a/Mapbox/MapboxMapsGestures/GestureManager.swift b/Mapbox/MapboxMapsGestures/GestureManager.swift index 712b3c5c12b8..d6b56af20a68 100644 --- a/Mapbox/MapboxMapsGestures/GestureManager.swift +++ b/Mapbox/MapboxMapsGestures/GestureManager.swift @@ -283,7 +283,7 @@ extension GestureManager: GestureHandlerDelegate { // Pan has ended on the MapView with a residual `offset` internal func panEnded(with offset: CGPoint) { - self.cameraManager.moveCamera(by: offset, animated: true) + self.cameraManager.moveCamera(by: offset, animated: true, didFling: true) } internal func cancelGestureTransitions() { From 521c818c1fb47a13a1f135591e408e4bc111df68 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Wed, 17 Feb 2021 11:13:11 -0500 Subject: [PATCH 05/12] fixing crash --- Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift b/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift index 8c80fec106fb..0947cda17b2f 100644 --- a/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift +++ b/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift @@ -465,11 +465,13 @@ public class CameraManager { return CLLocationCoordinate2D(latitude: 0, longitude: 0) } - var pitchFactor = mapView.pitch + var pitchFactor: CGFloat = mapView.pitch if didFling { if pitchFactor != 0.0 { pitchFactor /= 10.0 pitchFactor += 1.5 + } else { + pitchFactor = 1.0 } } else { pitchFactor = 1.0 // We do not want divide by 0 From 90e3b9f2ebf41dd0076cdeaec28ee6fd7eaa5d08 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Thu, 18 Feb 2021 14:46:07 -0500 Subject: [PATCH 06/12] updating docs --- Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift b/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift index 7bd29e9f0634..be30c4b96238 100644 --- a/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift +++ b/Mapbox/MapboxMapsFoundation/Camera/CameraManager.swift @@ -399,6 +399,7 @@ public class CameraManager { - Parameter pitch: The degrees to adjust the map's tilt by. - Parameter zoom: The amount to adjust the camera's zoom level by. - Parameter animated: Indicates whether the camera changes should be animated. + - Parameter didFling: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated */ public func moveCamera(by offset: CGPoint? = nil, rotation: CGFloat? = nil, pitch: CGFloat? = nil, zoom: CGFloat? = nil, animated: Bool = false, didFling: Bool = false) { guard let mapView = mapView else { @@ -454,6 +455,7 @@ public class CameraManager { /** Return a new center coordinate shifted by a given offset value. - Parameter offset: The `CGPoint` value to shift the map's center by. + - Parameter didFling: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated */ func shiftCenterCoordinate(by offset: CGPoint, didFling: Bool) -> CLLocationCoordinate2D { guard let mapView = mapView else { From e253b3f9ef72a739a8a171227f69b3885031d462 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Thu, 18 Feb 2021 15:09:06 -0500 Subject: [PATCH 07/12] fix unit tests --- .../Camera/MapboxMapsCameraTests.swift | 4 ++-- Mapbox/MapboxMapsGesturesTests/MockCameraManager.swift | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Mapbox/MapboxMapsFoundationTests/Camera/MapboxMapsCameraTests.swift b/Mapbox/MapboxMapsFoundationTests/Camera/MapboxMapsCameraTests.swift index 4d82ce85b7dd..cd1a85f5d663 100644 --- a/Mapbox/MapboxMapsFoundationTests/Camera/MapboxMapsCameraTests.swift +++ b/Mapbox/MapboxMapsFoundationTests/Camera/MapboxMapsCameraTests.swift @@ -321,7 +321,7 @@ class CameraManagerTests: XCTestCase { bearing: bearing, animated: false) - let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset) + let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, didFling: false) XCTAssertGreaterThan(shiftedCenterCoordinate.longitude, 180, line: line) } @@ -337,7 +337,7 @@ class CameraManagerTests: XCTestCase { bearing: bearing, animated: false) - let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset) + let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, didFling: false) XCTAssertLessThan(shiftedCenterCoordinate.longitude, -180, line: line) } diff --git a/Mapbox/MapboxMapsGesturesTests/MockCameraManager.swift b/Mapbox/MapboxMapsGesturesTests/MockCameraManager.swift index 1816f13a9f40..4e3a537c9a01 100644 --- a/Mapbox/MapboxMapsGesturesTests/MockCameraManager.swift +++ b/Mapbox/MapboxMapsGesturesTests/MockCameraManager.swift @@ -35,7 +35,8 @@ final class MockCameraManager: CameraManagerProtocol { rotation: CGFloat?, pitch: CGFloat?, zoom: CGFloat?, - animated: Bool) { + animated: Bool, + didFling: Bool) { } func cancelTransitions() { From d932fa02b5423eb3f5357874fc587d49dbd535d7 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Wed, 24 Feb 2021 09:13:02 -0500 Subject: [PATCH 08/12] supressing swift lint parameter count --- Sources/MapboxMaps/Gestures/GestureManager.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/MapboxMaps/Gestures/GestureManager.swift b/Sources/MapboxMaps/Gestures/GestureManager.swift index f690bf646251..0dd59474f583 100644 --- a/Sources/MapboxMaps/Gestures/GestureManager.swift +++ b/Sources/MapboxMaps/Gestures/GestureManager.swift @@ -119,6 +119,7 @@ internal protocol CameraManagerProtocol: AnyObject { duration: TimeInterval, completion: ((Bool) -> Void)?) + //swiftlint:disable function_parameter_count func moveCamera(by offset: CGPoint?, rotation: CGFloat?, pitch: CGFloat?, From daceb7990c8acfde242de4db2b6411570a51e6e4 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Wed, 24 Feb 2021 09:25:55 -0500 Subject: [PATCH 09/12] supressing another lint warning --- Tests/MapboxMapsTests/Gestures/MockCameraManager.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift b/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift index 4e3a537c9a01..b73ab4283f5a 100644 --- a/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift +++ b/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift @@ -31,6 +31,7 @@ final class MockCameraManager: CameraManagerProtocol { completion: completion)) } + //swiftlint:disable function_parameter_count func moveCamera(by offset: CGPoint?, rotation: CGFloat?, pitch: CGFloat?, From 62f05e152da32ec38bc8388bba25098d5809a9c2 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Wed, 24 Feb 2021 11:33:44 -0500 Subject: [PATCH 10/12] rename didFling to drift --- .../MapboxMaps/Foundation/Camera/CameraManager.swift | 12 ++++++------ Sources/MapboxMaps/Gestures/GestureManager.swift | 6 +++--- .../Foundation/Camera/MapboxMapsCameraTests.swift | 4 ++-- .../MapboxMapsTests/Gestures/MockCameraManager.swift | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift b/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift index 7cfbdb880b1b..6e1e32902d22 100644 --- a/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift +++ b/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift @@ -399,15 +399,15 @@ public class CameraManager { - Parameter pitch: The degrees to adjust the map's tilt by. - Parameter zoom: The amount to adjust the camera's zoom level by. - Parameter animated: Indicates whether the camera changes should be animated. - - Parameter didFling: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated + - Parameter drift: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated */ - public func moveCamera(by offset: CGPoint? = nil, rotation: CGFloat? = nil, pitch: CGFloat? = nil, zoom: CGFloat? = nil, animated: Bool = false, didFling: Bool = false) { + public func moveCamera(by offset: CGPoint? = nil, rotation: CGFloat? = nil, pitch: CGFloat? = nil, zoom: CGFloat? = nil, animated: Bool = false, drift: Bool = false) { guard let mapView = mapView else { assertionFailure("MapView is nil.") return } - let centerCoordinate = self.shiftCenterCoordinate(by: offset ?? .zero, didFling: didFling) + let centerCoordinate = self.shiftCenterCoordinate(by: offset ?? .zero, drift: drift) var newBearing: CGFloat = 0 if let angle = rotation { @@ -455,9 +455,9 @@ public class CameraManager { /** Return a new center coordinate shifted by a given offset value. - Parameter offset: The `CGPoint` value to shift the map's center by. - - Parameter didFling: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated + - Parameter drift: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated */ - func shiftCenterCoordinate(by offset: CGPoint, didFling: Bool) -> CLLocationCoordinate2D { + func shiftCenterCoordinate(by offset: CGPoint, drift: Bool) -> CLLocationCoordinate2D { guard let mapView = mapView else { assertionFailure("MapView is nil.") return CLLocationCoordinate2D(latitude: 0, longitude: 0) @@ -467,7 +467,7 @@ public class CameraManager { } var pitchFactor: CGFloat = mapView.pitch - if didFling { + if drift { if pitchFactor != 0.0 { pitchFactor /= 10.0 pitchFactor += 1.5 diff --git a/Sources/MapboxMaps/Gestures/GestureManager.swift b/Sources/MapboxMaps/Gestures/GestureManager.swift index 0dd59474f583..e02b912be5d4 100644 --- a/Sources/MapboxMaps/Gestures/GestureManager.swift +++ b/Sources/MapboxMaps/Gestures/GestureManager.swift @@ -125,7 +125,7 @@ internal protocol CameraManagerProtocol: AnyObject { pitch: CGFloat?, zoom: CGFloat?, animated: Bool, - didFling: Bool) + drift: Bool) func cancelTransitions() } @@ -313,12 +313,12 @@ extension GestureManager: GestureHandlerDelegate { // MapView has been panned internal func panned(by displacement: CGPoint) { - self.cameraManager.moveCamera(by: displacement, rotation: nil, pitch: nil, zoom: nil, animated: false, didFling: false) + self.cameraManager.moveCamera(by: displacement, rotation: nil, pitch: nil, zoom: nil, animated: false, drift: false) } // Pan has ended on the MapView with a residual `offset` internal func panEnded(with offset: CGPoint) { - self.cameraManager.moveCamera(by: offset, rotation: nil, pitch: nil, zoom: nil, animated: true, didFling: true) + self.cameraManager.moveCamera(by: offset, rotation: nil, pitch: nil, zoom: nil, animated: true, drift: true) } internal func cancelGestureTransitions() { diff --git a/Tests/MapboxMapsTests/Foundation/Camera/MapboxMapsCameraTests.swift b/Tests/MapboxMapsTests/Foundation/Camera/MapboxMapsCameraTests.swift index 9a5aef0de2d6..46e548e65c62 100644 --- a/Tests/MapboxMapsTests/Foundation/Camera/MapboxMapsCameraTests.swift +++ b/Tests/MapboxMapsTests/Foundation/Camera/MapboxMapsCameraTests.swift @@ -321,7 +321,7 @@ class CameraManagerTests: XCTestCase { bearing: bearing, animated: false) - let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, didFling: false) + let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, drift: false) XCTAssertGreaterThan(shiftedCenterCoordinate.longitude, 180, line: line) } @@ -337,7 +337,7 @@ class CameraManagerTests: XCTestCase { bearing: bearing, animated: false) - let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, didFling: false) + let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, drift: false) XCTAssertLessThan(shiftedCenterCoordinate.longitude, -180, line: line) } diff --git a/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift b/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift index b73ab4283f5a..856467629129 100644 --- a/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift +++ b/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift @@ -37,7 +37,7 @@ final class MockCameraManager: CameraManagerProtocol { pitch: CGFloat?, zoom: CGFloat?, animated: Bool, - didFling: Bool) { + drift: Bool) { } func cancelTransitions() { From 8cfcb9e768fd4627712e695b833cef003a4e230d Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Wed, 24 Feb 2021 15:17:58 -0500 Subject: [PATCH 11/12] pr comments --- Sources/MapboxMaps/Foundation/Camera/CameraManager.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift b/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift index 6e1e32902d22..912d255a69ca 100644 --- a/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift +++ b/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift @@ -457,7 +457,7 @@ public class CameraManager { - Parameter offset: The `CGPoint` value to shift the map's center by. - Parameter drift: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated */ - func shiftCenterCoordinate(by offset: CGPoint, drift: Bool) -> CLLocationCoordinate2D { + func shiftCenterCoordinate(by offset: CGPoint, drift: Bool = false) -> CLLocationCoordinate2D { guard let mapView = mapView else { assertionFailure("MapView is nil.") return CLLocationCoordinate2D(latitude: 0, longitude: 0) @@ -466,9 +466,11 @@ public class CameraManager { return CLLocationCoordinate2D(latitude: 0, longitude: 0) } + /// Stop gap solution until we adopt the Drag API var pitchFactor: CGFloat = mapView.pitch if drift { if pitchFactor != 0.0 { + // These calculations are creating a multiplier for the offset to normalize the offset for pitched maps pitchFactor /= 10.0 pitchFactor += 1.5 } else { From ad5fca9f51706aff3acf7dea16f2a6ddf1d91a34 Mon Sep 17 00:00:00 2001 From: Neel Mistry Date: Wed, 24 Feb 2021 15:25:42 -0500 Subject: [PATCH 12/12] more pr comments --- .../Foundation/Camera/CameraManager.swift | 14 +++++++------- Sources/MapboxMaps/Gestures/GestureManager.swift | 6 +++--- .../Foundation/Camera/MapboxMapsCameraTests.swift | 4 ++-- .../Gestures/MockCameraManager.swift | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift b/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift index 912d255a69ca..e91b544de563 100644 --- a/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift +++ b/Sources/MapboxMaps/Foundation/Camera/CameraManager.swift @@ -399,15 +399,15 @@ public class CameraManager { - Parameter pitch: The degrees to adjust the map's tilt by. - Parameter zoom: The amount to adjust the camera's zoom level by. - Parameter animated: Indicates whether the camera changes should be animated. - - Parameter drift: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated + - Parameter pitchedDrift: This hack indicates that the calling function wants to simulate drift. Therefore we need to do some additional calculations */ - public func moveCamera(by offset: CGPoint? = nil, rotation: CGFloat? = nil, pitch: CGFloat? = nil, zoom: CGFloat? = nil, animated: Bool = false, drift: Bool = false) { + public func moveCamera(by offset: CGPoint? = nil, rotation: CGFloat? = nil, pitch: CGFloat? = nil, zoom: CGFloat? = nil, animated: Bool = false, pitchedDrift: Bool = false) { guard let mapView = mapView else { assertionFailure("MapView is nil.") return } - let centerCoordinate = self.shiftCenterCoordinate(by: offset ?? .zero, drift: drift) + let centerCoordinate = self.shiftCenterCoordinate(by: offset ?? .zero, pitchedDrift: pitchedDrift) var newBearing: CGFloat = 0 if let angle = rotation { @@ -455,9 +455,9 @@ public class CameraManager { /** Return a new center coordinate shifted by a given offset value. - Parameter offset: The `CGPoint` value to shift the map's center by. - - Parameter drift: Indicates if the camera is experiencing a fling, which means the offset value needs to be manipulated + - Parameter pitchedDrift: This hack indicates that the calling function wants to simulate drift. Therefore we need to do some additional calculations */ - func shiftCenterCoordinate(by offset: CGPoint, drift: Bool = false) -> CLLocationCoordinate2D { + func shiftCenterCoordinate(by offset: CGPoint, pitchedDrift: Bool = false) -> CLLocationCoordinate2D { guard let mapView = mapView else { assertionFailure("MapView is nil.") return CLLocationCoordinate2D(latitude: 0, longitude: 0) @@ -466,9 +466,9 @@ public class CameraManager { return CLLocationCoordinate2D(latitude: 0, longitude: 0) } - /// Stop gap solution until we adopt the Drag API + /// Stop gap solution until we land on a fix all var pitchFactor: CGFloat = mapView.pitch - if drift { + if pitchedDrift { if pitchFactor != 0.0 { // These calculations are creating a multiplier for the offset to normalize the offset for pitched maps pitchFactor /= 10.0 diff --git a/Sources/MapboxMaps/Gestures/GestureManager.swift b/Sources/MapboxMaps/Gestures/GestureManager.swift index e02b912be5d4..303fd9f5a004 100644 --- a/Sources/MapboxMaps/Gestures/GestureManager.swift +++ b/Sources/MapboxMaps/Gestures/GestureManager.swift @@ -125,7 +125,7 @@ internal protocol CameraManagerProtocol: AnyObject { pitch: CGFloat?, zoom: CGFloat?, animated: Bool, - drift: Bool) + pitchedDrift: Bool) func cancelTransitions() } @@ -313,12 +313,12 @@ extension GestureManager: GestureHandlerDelegate { // MapView has been panned internal func panned(by displacement: CGPoint) { - self.cameraManager.moveCamera(by: displacement, rotation: nil, pitch: nil, zoom: nil, animated: false, drift: false) + self.cameraManager.moveCamera(by: displacement, rotation: nil, pitch: nil, zoom: nil, animated: false, pitchedDrift: false) } // Pan has ended on the MapView with a residual `offset` internal func panEnded(with offset: CGPoint) { - self.cameraManager.moveCamera(by: offset, rotation: nil, pitch: nil, zoom: nil, animated: true, drift: true) + self.cameraManager.moveCamera(by: offset, rotation: nil, pitch: nil, zoom: nil, animated: true, pitchedDrift: true) } internal func cancelGestureTransitions() { diff --git a/Tests/MapboxMapsTests/Foundation/Camera/MapboxMapsCameraTests.swift b/Tests/MapboxMapsTests/Foundation/Camera/MapboxMapsCameraTests.swift index 46e548e65c62..1d93c5adb618 100644 --- a/Tests/MapboxMapsTests/Foundation/Camera/MapboxMapsCameraTests.swift +++ b/Tests/MapboxMapsTests/Foundation/Camera/MapboxMapsCameraTests.swift @@ -321,7 +321,7 @@ class CameraManagerTests: XCTestCase { bearing: bearing, animated: false) - let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, drift: false) + let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, pitchedDrift: false) XCTAssertGreaterThan(shiftedCenterCoordinate.longitude, 180, line: line) } @@ -337,7 +337,7 @@ class CameraManagerTests: XCTestCase { bearing: bearing, animated: false) - let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, drift: false) + let shiftedCenterCoordinate = cameraManager.shiftCenterCoordinate(by: offset, pitchedDrift: false) XCTAssertLessThan(shiftedCenterCoordinate.longitude, -180, line: line) } diff --git a/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift b/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift index 856467629129..3a676fd904d5 100644 --- a/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift +++ b/Tests/MapboxMapsTests/Gestures/MockCameraManager.swift @@ -37,7 +37,7 @@ final class MockCameraManager: CameraManagerProtocol { pitch: CGFloat?, zoom: CGFloat?, animated: Bool, - drift: Bool) { + pitchedDrift: Bool) { } func cancelTransitions() {