[ios] Fix camera paddings reset on map view gestures performing. (#188)

* Fix camera paddings reset on map view gestures performing.

* Asymmetrical paddings fix demo app.

* Remove the demo for the PR branch.
This commit is contained in:
Oleksandr Stepanov
2021-12-02 16:39:58 +02:00
committed by GitHub
parent 004a849d8c
commit ba1ec44061
5 changed files with 76 additions and 64 deletions

View File

@@ -108,6 +108,10 @@ NS_INLINE mbgl::EdgeInsets MGLEdgeInsetsFromNSEdgeInsets(MGLEdgeInsets insets) {
return { insets.top, insets.left, insets.bottom, insets.right };
}
NS_INLINE MGLEdgeInsets NSEdgeInsetsFromMGLEdgeInsets(const mbgl::EdgeInsets& insets) {
return MGLEdgeInsetsMake(insets.top(), insets.left(), insets.bottom(), insets.right());
}
/// Returns the combination of two edge insets.
NS_INLINE MGLEdgeInsets MGLEdgeInsetsInsetEdgeInset(MGLEdgeInsets base, MGLEdgeInsets inset) {
return MGLEdgeInsetsMake(base.top + inset.top,

View File

@@ -1386,6 +1386,19 @@ MGL_EXPORT
*/
@property (nonatomic, assign) UIEdgeInsets contentInset;
/**
The current edge insets of the current map view’s camera.
Camera edge insets are formed as accumulation of map view's content insets
and the edge padding passed to the method like `seCamera:...edgePadding:`,
`setVisibleCoordinates:...edgePadding:`, `showAnnotations:...edgePadding:` etc.
The camera edge insets influences the `centerCoordinate` of the viewport.
This value is read-only, in order to apply paddings, use either persistent
`contentInset`, either transient `edgePadding` parameter of the `set...` methods.
*/
@property (nonatomic, readonly) UIEdgeInsets cameraEdgeInsets;
/**
Deprecated. Sets the distance from the edges of the map view’s frame to the edges
of the map view’s logical viewport with an optional transition animation.

View File

@@ -2084,8 +2084,7 @@ public:
{
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }));
// The gesture recognizer only reports the gesture’s current center
// point, so use the previous center point to anchor the transition.
@@ -2145,8 +2144,8 @@ public:
{
self.mbglMap.easeTo(mbgl::CameraOptions()
.withZoom(zoom)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)), MGLDurationFromTimeInterval(duration));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }),
MGLDurationFromTimeInterval(duration));
}
}
@@ -2217,8 +2216,7 @@ public:
{
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withBearing(newDegrees)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y})
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y}));
}
[self cameraIsChanging];
@@ -2259,8 +2257,7 @@ public:
{
self.mbglMap.easeTo(mbgl::CameraOptions()
.withBearing(newDegrees)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)),
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }),
MGLDurationFromTimeInterval(decelerationRate));
[self notifyGestureDidEndWithDrift:YES];
@@ -2390,8 +2387,8 @@ public:
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
self.mbglMap.easeTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(center)
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)), MGLDurationFromTimeInterval(MGLAnimationDuration));
.withAnchor(center),
MGLDurationFromTimeInterval(MGLAnimationDuration));
__weak MGLMapView *weakSelf = self;
@@ -2431,8 +2428,8 @@ public:
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
self.mbglMap.easeTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(center)
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)), MGLDurationFromTimeInterval(MGLAnimationDuration));
.withAnchor(center),
MGLDurationFromTimeInterval(MGLAnimationDuration));
__weak MGLMapView *weakSelf = self;
@@ -2476,8 +2473,7 @@ public:
{
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }));
}
[self cameraIsChanging];
@@ -2544,8 +2540,7 @@ public:
{
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withPitch(pitchNew)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }));
}
[self cameraIsChanging];
@@ -2591,8 +2586,7 @@ public:
- (MGLMapCamera *)cameraByRotatingToDirection:(CLLocationDirection)degrees aroundAnchorPoint:(CGPoint)anchorPoint
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::CameraOptions currentCameraOptions = self.mbglMap.getCameraOptions(padding);
mbgl::CameraOptions currentCameraOptions = self.mbglMap.getCameraOptions();
MGLMapCamera *camera;
@@ -2606,8 +2600,7 @@ public:
- (MGLMapCamera *)cameraByTiltingToPitch:(CGFloat)pitch
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::CameraOptions currentCameraOptions = self.mbglMap.getCameraOptions(padding);
mbgl::CameraOptions currentCameraOptions = self.mbglMap.getCameraOptions();
MGLMapCamera *camera;
@@ -3548,8 +3541,7 @@ static void *windowScreenContext = &windowScreenContext;
double newZoom = round(self.zoomLevel) + log2(scaleFactor);
self.mbglMap.jumpTo(mbgl::CameraOptions()
.withZoom(newZoom)
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y })
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)));
.withAnchor(mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }));
[self unrotateIfNeededForGesture];
_accessibilityValueAnnouncementIsPending = YES;
@@ -3576,8 +3568,7 @@ static void *windowScreenContext = &windowScreenContext;
- (CLLocationCoordinate2D)centerCoordinate
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
return MGLLocationCoordinate2DFromLatLng(*self.mbglMap.getCameraOptions(padding).center);
return MGLLocationCoordinate2DFromLatLng(*self.mbglMap.getCameraOptions().center);
}
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(double)zoomLevel animated:(BOOL)animated
@@ -3687,8 +3678,7 @@ static void *windowScreenContext = &windowScreenContext;
- (double)zoomLevel
{
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
return *self.mbglMap.getCameraOptions(padding).zoom;
return *self.mbglMap.getCameraOptions().zoom;
}
- (void)setZoomLevel:(double)zoomLevel
@@ -3708,9 +3698,7 @@ static void *windowScreenContext = &windowScreenContext;
CGFloat duration = animated ? MGLAnimationDuration : 0;
self.mbglMap.easeTo(mbgl::CameraOptions()
.withZoom(zoomLevel)
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)),
MGLDurationFromTimeInterval(duration));
.withZoom(zoomLevel), MGLDurationFromTimeInterval(duration));
}
- (void)setMinimumZoomLevel:(double)minimumZoomLevel
@@ -3938,9 +3926,7 @@ static void *windowScreenContext = &windowScreenContext;
if (self.userTrackingMode == MGLUserTrackingModeNone)
{
self.mbglMap.easeTo(mbgl::CameraOptions()
.withBearing(direction)
.withPadding(MGLEdgeInsetsFromNSEdgeInsets(self.contentInset)),
self.mbglMap.easeTo(mbgl::CameraOptions().withBearing(direction),
MGLDurationFromTimeInterval(duration));
}
else
@@ -3969,6 +3955,11 @@ static void *windowScreenContext = &windowScreenContext;
return [NSSet setWithObjects:@"longitude", @"latitude", @"centerCoordinate", @"zoomLevel", @"direction", nil];
}
- (UIEdgeInsets)cameraEdgeInsets {
mbgl::CameraOptions cameraOptions = self.mbglMap.getCameraOptions();
return NSEdgeInsetsFromMGLEdgeInsets(cameraOptions.padding.value_or(mbgl::EdgeInsets()));
}
- (MGLMapCamera *)camera
{
if (!_mbglMap)
@@ -3977,8 +3968,7 @@ static void *windowScreenContext = &windowScreenContext;
return self.residualCamera;
}
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
return [self cameraForCameraOptions:self.mbglMap.getCameraOptions(padding)];
return [self cameraForCameraOptions:self.mbglMap.getCameraOptions()];
}
- (void)setCamera:(MGLMapCamera *)camera

View File

@@ -72,8 +72,6 @@
(self.screenBounds.size.height/2) + ((contentInset.top - contentInset.bottom) / 2));
XCTAssertEqualWithAccuracy(shiftedPoint.x, expectedShiftedPoint.x, 0.01);
XCTAssertEqualWithAccuracy(shiftedPoint.y, expectedShiftedPoint.y, 0.01);
}
- (void)testContentInsetOrnaments {
@@ -171,7 +169,6 @@
y = self.screenBounds.size.height - attributionView.bounds.size.height - margin;
expectedAttributionOrigin = CGPointMake(x, y);
XCTAssertTrue(CGPointEqualToPoint(attributionView.frame.origin, expectedAttributionOrigin));
}
@end

View File

@@ -67,7 +67,7 @@
}
- (void)testHandlePinchGestureContentInset {
UIEdgeInsets contentInset = UIEdgeInsetsZero;
UIEdgeInsets contentInset = UIEdgeInsetsMake(1, 1, 1, 1);
self.mapView.contentInset = contentInset;
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.mapView.contentInset);
auto cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -75,7 +75,7 @@
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
[self.mapView setCamera:self.mapView.camera withDuration:0.1 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
[self.mapView setCamera:self.mapView.camera withDuration:0.0 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
XCTAssertFalse(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -86,21 +86,22 @@
pinchGesture.scale = 1.0;
[self.mapView handlePinchGesture:pinchGesture];
XCTAssertNotEqual(padding, cameraPadding);
mbgl::EdgeInsets edgePadding = MGLEdgeInsetsFromNSEdgeInsets(contentInset) + padding;
pinchGesture.state = UIGestureRecognizerStateChanged;
[self.mapView handlePinchGesture:pinchGesture];
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
pinchGesture.state = UIGestureRecognizerStateEnded;
[self.mapView handlePinchGesture:pinchGesture];
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
}
- (void)testHandleRotateGestureContentInset {
UIEdgeInsets contentInset = UIEdgeInsetsZero;
UIEdgeInsets contentInset = UIEdgeInsetsMake(1, 1, 1, 1);
self.mapView.contentInset = contentInset;
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.mapView.contentInset);
auto cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -108,7 +109,7 @@
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
[self.mapView setCamera:self.mapView.camera withDuration:0.1 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
[self.mapView setCamera:self.mapView.camera withDuration:0.0 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
XCTAssertFalse(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -119,17 +120,18 @@
rotateGesture.rotation = 1;
[self.mapView handleRotateGesture:rotateGesture];
XCTAssertNotEqual(padding, cameraPadding);
mbgl::EdgeInsets edgePadding = MGLEdgeInsetsFromNSEdgeInsets(contentInset) + padding;
rotateGesture.state = UIGestureRecognizerStateChanged;
[self.mapView handleRotateGesture:rotateGesture];
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
rotateGesture.state = UIGestureRecognizerStateEnded;
[self.mapView handleRotateGesture:rotateGesture];
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
}
- (void)testHandleDoubleTapGestureContentInset {
@@ -141,7 +143,7 @@
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
[self.mapView setCamera:self.mapView.camera withDuration:0.1 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
[self.mapView setCamera:self.mapView.camera withDuration:0.0 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
XCTAssertFalse(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -150,6 +152,8 @@
UITapGestureRecognizerMock *doubleTapGesture = [[UITapGestureRecognizerMock alloc] initWithTarget:nil action:nil];
doubleTapGesture.mockTappedView = self.mapView;
doubleTapGesture.mockTappedPoint = CGPointMake(1.0, 1.0);
mbgl::EdgeInsets edgePadding = MGLEdgeInsetsFromNSEdgeInsets(contentInset) + padding;
[self.mapView handleDoubleTapGesture:doubleTapGesture];
_doubleTapExpectation = [self expectationWithDescription:@"Double tap gesture animation."];
@@ -160,12 +164,11 @@
[self waitForExpectationsWithTimeout:10 handler:nil];
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
}
- (void)testHandleTwoFingerTapGesture {
UIEdgeInsets contentInset = UIEdgeInsetsZero;
UIEdgeInsets contentInset = UIEdgeInsetsMake(1, 1, 1, 1);
self.mapView.contentInset = contentInset;
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.mapView.contentInset);
auto cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -173,7 +176,7 @@
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
[self.mapView setCamera:self.mapView.camera withDuration:0.1 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
[self.mapView setCamera:self.mapView.camera withDuration:0.0 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
XCTAssertFalse(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -190,13 +193,14 @@
[self->_twoFingerExpectation fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:nil];
mbgl::EdgeInsets edgePadding = MGLEdgeInsetsFromNSEdgeInsets(contentInset) + padding;
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
}
- (void)testHandleQuickZoomGesture {
UIEdgeInsets contentInset = UIEdgeInsetsZero;
UIEdgeInsets contentInset = UIEdgeInsetsMake(1, 1, 1, 1);
self.mapView.contentInset = contentInset;
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.mapView.contentInset);
auto cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -204,7 +208,7 @@
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
[self.mapView setCamera:self.mapView.camera withDuration:0.1 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
[self.mapView setCamera:self.mapView.camera withDuration:0.0 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
XCTAssertFalse(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -224,18 +228,20 @@
[self->_quickZoomExpectation fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:nil];
mbgl::EdgeInsets edgePadding = MGLEdgeInsetsFromNSEdgeInsets(contentInset) + padding;
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
quickZoom.state = UIGestureRecognizerStateEnded;
[self.mapView handleQuickZoomGesture:quickZoom];
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
}
- (void)testHandleTwoFingerDragGesture {
UIEdgeInsets contentInset = UIEdgeInsetsZero;
UIEdgeInsets contentInset = UIEdgeInsetsMake(1, 1, 1, 1);
self.mapView.contentInset = contentInset;
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.mapView.contentInset);
auto cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -243,7 +249,7 @@
XCTAssertTrue(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
[self.mapView setCamera:self.mapView.camera withDuration:0.1 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
[self.mapView setCamera:self.mapView.camera withDuration:0.0 animationTimingFunction:nil edgePadding:contentInset completionHandler:nil];
XCTAssertFalse(UIEdgeInsetsEqualToEdgeInsets(self.mapView.contentInset, contentInset));
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
@@ -267,14 +273,16 @@
[self->_twoFingerDragExpectation fulfill];
});
[self waitForExpectationsWithTimeout:10 handler:nil];
mbgl::EdgeInsets edgePadding = MGLEdgeInsetsFromNSEdgeInsets(contentInset) + padding;
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
twoFingerDrag.state = UIGestureRecognizerStateEnded;
[self.mapView handleTwoFingerDragGesture:twoFingerDrag];
cameraPadding = self.mapView.mbglMap.getCameraOptions().padding;
XCTAssertEqual(padding, cameraPadding, @"When a gesture recognizer is performed contentInsets and camera padding should match.");
XCTAssertEqual(edgePadding, cameraPadding, @"When a gesture recognizer is performed camera paddings must not be changed.");
}
@end