Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 082e898

Browse files
committed
[ios] code refactoring to clarify the conditions to execute a gesture
1 parent be62f8c commit 082e898

File tree

2 files changed

+30
-64
lines changed

2 files changed

+30
-64
lines changed

platform/ios/src/MGLMapView.mm

+30-63
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,6 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
12061206
_mbglMap->cancelTransitions();
12071207

12081208
MGLMapCamera *oldCamera = self.camera;
1209-
MGLMapCamera *toCamera;
12101209

12111210
if (pan.state == UIGestureRecognizerStateBegan)
12121211
{
@@ -1220,13 +1219,11 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
12201219
{
12211220
CGPoint delta = [pan translationInView:pan.view];
12221221

1223-
toCamera = [self cameraByPanningWithTranslation:delta panGesture:pan];
1222+
MGLMapCamera *toCamera = [self cameraByPanningWithTranslation:delta panGesture:pan];
12241223

1225-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1226-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1224+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1225+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
12271226
{
1228-
self.camera = oldCamera;
1229-
} else {
12301227
_mbglMap->moveBy({ delta.x, delta.y });
12311228
[pan setTranslation:CGPointZero inView:pan.view];
12321229
}
@@ -1246,13 +1243,11 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
12461243
if (drift)
12471244
{
12481245
CGPoint offset = CGPointMake(velocity.x * self.decelerationRate / 4, velocity.y * self.decelerationRate / 4);
1249-
toCamera = [self cameraByPanningWithTranslation:offset panGesture:pan];
1246+
MGLMapCamera *toCamera = [self cameraByPanningWithTranslation:offset panGesture:pan];
12501247

1251-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1252-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1248+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1249+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
12531250
{
1254-
self.camera = oldCamera;
1255-
} else {
12561251
_mbglMap->moveBy({ offset.x, offset.y }, MGLDurationInSecondsFromTimeInterval(self.decelerationRate));
12571252
}
12581253
}
@@ -1295,19 +1290,15 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
12951290
else if (pinch.state == UIGestureRecognizerStateChanged)
12961291
{
12971292
CGFloat newScale = self.scale * pinch.scale;
1298-
1299-
if (log2(newScale) < _mbglMap->getMinZoom()) return;
1293+
double zoom = log2(newScale);
1294+
if (zoom < _mbglMap->getMinZoom()) return;
13001295

13011296
// Calculates the final camera zoom, has no effect within current map camera.
1302-
MGLMapCamera *toCamera;
1303-
double zoom = log2(newScale);
1304-
toCamera = [self cameraByZoomingToZoomLevel:zoom aroundAnchorPoint:centerPoint];
1297+
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:zoom aroundAnchorPoint:centerPoint];
13051298

1306-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1307-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1299+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1300+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
13081301
{
1309-
self.camera = oldCamera;
1310-
} else {
13111302
_mbglMap->setScale(newScale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
13121303
}
13131304
// The gesture recognizer only reports the gesture’s current center
@@ -1356,14 +1347,12 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
13561347
BOOL drift = velocity && duration;
13571348

13581349
// Calculates the final camera zoom, this has no effect within current map camera.
1359-
MGLMapCamera *toCamera;
13601350
double zoom = log2(newScale);
1361-
toCamera = [self cameraByZoomingToZoomLevel:zoom aroundAnchorPoint:centerPoint];
1351+
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:zoom aroundAnchorPoint:centerPoint];
13621352

13631353
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
13641354
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
13651355
{
1366-
self.camera = oldCamera;
13671356
drift = NO;
13681357
} else {
13691358
if (drift)
@@ -1388,8 +1377,6 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
13881377

13891378
CGPoint centerPoint = [self anchorPointForGesture:rotate];
13901379
MGLMapCamera *oldCamera = self.camera;
1391-
MGLMapCamera *toCamera;
1392-
13931380

13941381
if (rotate.state == UIGestureRecognizerStateBegan)
13951382
{
@@ -1416,14 +1403,12 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
14161403
newDegrees = fmaxf(newDegrees, -30);
14171404
}
14181405

1419-
toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];
1406+
MGLMapCamera *toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];
14201407

1421-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1422-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1408+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1409+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
14231410
{
1424-
self.camera = oldCamera;
1425-
} else {
1426-
_mbglMap->setBearing(newDegrees, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
1411+
_mbglMap->setBearing(newDegrees, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
14271412
}
14281413

14291414
[self notifyMapChange:mbgl::MapChangeRegionIsChanging];
@@ -1439,13 +1424,11 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
14391424
CGFloat newRadians = radians + velocity * decelerationRate * 0.1;
14401425
CGFloat newDegrees = MGLDegreesFromRadians(newRadians) * -1;
14411426

1442-
toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];
1427+
MGLMapCamera *toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];
14431428

1444-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1445-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1429+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1430+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
14461431
{
1447-
self.camera = oldCamera;
1448-
} else {
14491432
_mbglMap->setBearing(newDegrees, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }, MGLDurationInSecondsFromTimeInterval(decelerationRate));
14501433

14511434
[self notifyGestureDidEndWithDrift:YES];
@@ -1457,7 +1440,6 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
14571440
[weakSelf unrotateIfNeededForGesture];
14581441
}];
14591442
}
1460-
14611443
}
14621444
else
14631445
{
@@ -1575,17 +1557,13 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
15751557
{
15761558
MGLMapCamera *oldCamera = self.camera;
15771559

1578-
double zoom = self.zoomLevel;
1579-
double newZoom = zoom + 1.0;
15801560
CGPoint gesturePoint = [self anchorPointForGesture:doubleTap];
15811561

1582-
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];
1562+
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:self.zoomLevel + 1.0 aroundAnchorPoint:gesturePoint];
15831563

1584-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1585-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1564+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1565+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
15861566
{
1587-
self.camera = oldCamera;
1588-
} else {
15891567
[self trackGestureEvent:MGLEventGestureDoubleTap forRecognizer:doubleTap];
15901568

15911569
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
@@ -1598,7 +1576,6 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
15981576
[weakSelf unrotateIfNeededForGesture];
15991577
}];
16001578
}
1601-
16021579
}
16031580
}
16041581

@@ -1619,17 +1596,13 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
16191596
MGLMapCamera *oldCamera = self.camera;
16201597

16211598
double zoom = self.zoomLevel;
1622-
double newZoom = zoom - 1.0;
16231599
CGPoint gesturePoint = [self anchorPointForGesture:twoFingerTap];
16241600

1625-
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];
1601+
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:zoom - 1.0 aroundAnchorPoint:gesturePoint];
16261602

1627-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1628-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1603+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1604+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
16291605
{
1630-
self.camera = oldCamera;
1631-
} else {
1632-
16331606
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
16341607
_mbglMap->scaleBy(0.5, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));
16351608

@@ -1640,7 +1613,6 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
16401613
[weakSelf unrotateIfNeededForGesture];
16411614
}];
16421615
}
1643-
16441616
}
16451617
}
16461618

@@ -1679,11 +1651,9 @@ - (void)handleQuickZoomGesture:(UILongPressGestureRecognizer *)quickZoom
16791651

16801652
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:estimatedZoom aroundAnchorPoint:centerPoint];
16811653

1682-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1683-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1654+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1655+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
16841656
{
1685-
self.camera = oldCamera;
1686-
} else {
16871657
_mbglMap->scaleBy(scale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
16881658
}
16891659

@@ -1702,7 +1672,6 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag
17021672

17031673
_mbglMap->cancelTransitions();
17041674
MGLMapCamera *oldCamera = self.camera;
1705-
MGLMapCamera *toCamera;
17061675

17071676
if (twoFingerDrag.state == UIGestureRecognizerStateBegan)
17081677
{
@@ -1719,13 +1688,11 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag
17191688

17201689
CGPoint centerPoint = [self anchorPointForGesture:twoFingerDrag];
17211690

1722-
toCamera = [self cameraByTiltingToPitch:pitchNew];
1691+
MGLMapCamera *toCamera = [self cameraByTiltingToPitch:pitchNew];
17231692

1724-
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
1725-
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
1693+
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
1694+
[self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
17261695
{
1727-
self.camera = oldCamera;
1728-
} else {
17291696
_mbglMap->setPitch(pitchNew, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
17301697
}
17311698

platform/macos/CHANGELOG.md

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
* Fixed an issue where translucent point annotations along tile boundaries would be drawn darker than expected. ([#6832](https://github.com/mapbox/mapbox-gl-native/pull/6832))
3636
* Fixed flickering that occurred when panning past the antimeridian. ([#7574](https://github.com/mapbox/mapbox-gl-native/pull/7574))
37-
* Fixed an issue that could prevent a cached style from appearing while the computer is offline. ([#7770](https://github.com/mapbox/mapbox-gl-native/pull/7770))
3837
* Added a method to MGLMapViewDelegate, `-mapView:shouldChangeFromCamera:toCamera:`, that you can implement to restrict which parts the user can navigate to using gestures. ([#5584](https://github.com/mapbox/mapbox-gl-native/pull/5584))
3938
* Added a `MGLDistanceFormatter` class for formatting geographic distances. ([#7888](https://github.com/mapbox/mapbox-gl-native/pull/7888))
4039

0 commit comments

Comments
 (0)