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

Commit 97e99e8

Browse files
committed
[ios] Removed duplicated methods, improved code readability
1 parent 9363cb4 commit 97e99e8

File tree

1 file changed

+31
-55
lines changed

1 file changed

+31
-55
lines changed

platform/ios/src/MGLMapView.mm

+31-55
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
12201220
{
12211221
CGPoint delta = [pan translationInView:pan.view];
12221222

1223-
toCamera = [self currentCameraWithEstimatedEndPoint:delta panGesture:pan];
1223+
toCamera = [self cameraByPanningWithTranslation:delta panGesture:pan];
12241224

12251225
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
12261226
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
@@ -1246,7 +1246,7 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
12461246
if (drift)
12471247
{
12481248
CGPoint offset = CGPointMake(velocity.x * self.decelerationRate / 4, velocity.y * self.decelerationRate / 4);
1249-
toCamera = [self currentCameraWithEstimatedEndPoint:offset panGesture:pan];
1249+
toCamera = [self cameraByPanningWithTranslation:offset panGesture:pan];
12501250

12511251
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
12521252
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
@@ -1283,8 +1283,6 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
12831283

12841284
CGPoint centerPoint = [self anchorPointForGesture:pinch];
12851285
MGLMapCamera *oldCamera = self.camera;
1286-
MGLMapCamera *toCamera;
1287-
double zoom = 0.0;
12881286

12891287
if (pinch.state == UIGestureRecognizerStateBegan)
12901288
{
@@ -1301,8 +1299,9 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
13011299
if (log2(newScale) < _mbglMap->getMinZoom()) return;
13021300

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

13071306
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
13081307
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
@@ -1357,15 +1356,16 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
13571356
BOOL drift = velocity && duration;
13581357

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

13631363
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
13641364
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
13651365
{
13661366
self.camera = oldCamera;
13671367
drift = NO;
1368-
}else {
1368+
} else {
13691369
if (drift)
13701370
{
13711371
_mbglMap->setScale(newScale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }, MGLDurationInSecondsFromTimeInterval(duration));
@@ -1416,7 +1416,7 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
14161416
newDegrees = fmaxf(newDegrees, -30);
14171417
}
14181418

1419-
toCamera = [self currentCameraWithEstimatedDegrees:newDegrees anchorPoint:centerPoint];
1419+
toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];
14201420

14211421
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
14221422
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
@@ -1439,7 +1439,7 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
14391439
CGFloat newRadians = radians + velocity * decelerationRate * 0.1;
14401440
CGFloat newDegrees = MGLDegreesFromRadians(newRadians) * -1;
14411441

1442-
toCamera = [self currentCameraWithEstimatedDegrees:newDegrees anchorPoint:centerPoint];
1442+
toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];
14431443

14441444
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
14451445
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
@@ -1575,18 +1575,18 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
15751575
{
15761576
MGLMapCamera *oldCamera = self.camera;
15771577

1578-
double zoom = [self currentCameraZoom];
1578+
double zoom = self.zoomLevel;
15791579
double newZoom = zoom + 1.0;
1580+
CGPoint gesturePoint = [self anchorPointForGesture:doubleTap];
15801581

1581-
MGLMapCamera *toCamera = [self currentCameraWithEstimatedZoom:newZoom];
1582+
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];
15821583

15831584
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
15841585
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
15851586
{
15861587
self.camera = oldCamera;
15871588
} else {
15881589
[self trackGestureEvent:MGLEventGestureDoubleTap forRecognizer:doubleTap];
1589-
CGPoint gesturePoint = [self anchorPointForGesture:doubleTap];
15901590

15911591
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
15921592
_mbglMap->scaleBy(2, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));
@@ -1618,17 +1618,17 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
16181618
{
16191619
MGLMapCamera *oldCamera = self.camera;
16201620

1621-
double zoom = [self currentCameraZoom];
1621+
double zoom = self.zoomLevel;
16221622
double newZoom = zoom - 1.0;
1623+
CGPoint gesturePoint = [self anchorPointForGesture:twoFingerTap];
16231624

1624-
MGLMapCamera *toCamera = [self currentCameraWithEstimatedZoom:newZoom];
1625+
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];
16251626

16261627
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
16271628
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
16281629
{
16291630
self.camera = oldCamera;
16301631
} else {
1631-
CGPoint gesturePoint = [self anchorPointForGesture:twoFingerTap];
16321632

16331633
mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
16341634
_mbglMap->scaleBy(0.5, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));
@@ -1672,18 +1672,18 @@ - (void)handleQuickZoomGesture:(UILongPressGestureRecognizer *)quickZoom
16721672

16731673
MGLMapCamera *oldCamera = self.camera;
16741674

1675-
double zoom = [self currentCameraZoom];
1675+
double zoom = self.zoomLevel;
16761676
double scale = powf(2, newZoom) / _mbglMap->getScale();
16771677

16781678
double estimatedZoom = zoom * scale;
16791679

1680-
MGLMapCamera *toCamera = [self currentCameraWithEstimatedZoom:estimatedZoom];
1680+
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:estimatedZoom aroundAnchorPoint:centerPoint];
16811681

16821682
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
16831683
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
16841684
{
16851685
self.camera = oldCamera;
1686-
}else {
1686+
} else {
16871687
_mbglMap->scaleBy(scale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
16881688
}
16891689

@@ -1719,7 +1719,7 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag
17191719

17201720
CGPoint centerPoint = [self anchorPointForGesture:twoFingerDrag];
17211721

1722-
toCamera = [self currentCameraWithEstimatedPitch:pitchNew anchorPoint:centerPoint];
1722+
toCamera = [self cameraByTiltingToPitch:pitchNew];
17231723

17241724
if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
17251725
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
@@ -1739,43 +1739,34 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag
17391739

17401740
}
17411741

1742-
- (MGLMapCamera *)currentCameraWithEstimatedEndPoint:(CGPoint)endPoint panGesture:(UIPanGestureRecognizer *)pan
1742+
- (MGLMapCamera *)cameraByPanningWithTranslation:(CGPoint)endPoint panGesture:(UIPanGestureRecognizer *)pan
17431743
{
17441744
MGLMapCamera *panCamera = [self.camera copy];
1745-
CGFloat width = CGRectGetWidth(self.bounds);
1746-
CGFloat height = CGRectGetHeight(self.bounds);
17471745

1748-
mbgl::ScreenCoordinate screenCenter = {
1749-
width/2.,
1750-
height/2.,
1751-
};
1752-
1753-
mbgl::ScreenCoordinate centerOffset = {
1754-
endPoint.x,
1755-
-endPoint.y,
1756-
};
1757-
1758-
mbgl::ScreenCoordinate centerPoint = screenCenter - centerOffset;
1759-
CGPoint endCameraPoint = CGPointMake(centerPoint.x, centerPoint.y);
1746+
CGPoint centerPoint = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
1747+
CGPoint endCameraPoint = CGPointMake(centerPoint.x - endPoint.x, centerPoint.y - endPoint.y);
17601748
CLLocationCoordinate2D panCoordinate = [self convertPoint:endCameraPoint toCoordinateFromView:pan.view];
1749+
17611750
panCamera.centerCoordinate = panCoordinate;
17621751

17631752
return panCamera;
17641753
}
17651754

1766-
- (MGLMapCamera *)currentCameraWithEstimatedZoom:(double)zoom
1755+
- (MGLMapCamera *)cameraByZoomingToZoomLevel:(double)zoom aroundAnchorPoint:(CGPoint)anchorPoint
17671756
{
17681757
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
17691758
mbgl::CameraOptions currentCameraOptions = _mbglMap->getCameraOptions(padding);
17701759
MGLMapCamera *camera;
17711760

1761+
mbgl::ScreenCoordinate anchor = mbgl::ScreenCoordinate { anchorPoint.x, anchorPoint.y };
17721762
currentCameraOptions.zoom = mbgl::util::clamp(zoom, self.minimumZoomLevel, self.maximumZoomLevel);
1763+
currentCameraOptions.anchor = anchor;
17731764
camera = [self cameraForCameraOptions:currentCameraOptions];
17741765

17751766
return camera;
17761767
}
17771768

1778-
- (MGLMapCamera *)currentCameraWithEstimatedDegrees:(CGFloat)degrees anchorPoint:(CGPoint)anchorPoint
1769+
- (MGLMapCamera *)cameraByRotatingToDirection:(CLLocationDirection)degrees aroundAnchorPoint:(CGPoint)anchorPoint
17791770
{
17801771
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
17811772
mbgl::CameraOptions currentCameraOptions = _mbglMap->getCameraOptions(padding);
@@ -1790,34 +1781,19 @@ - (MGLMapCamera *)currentCameraWithEstimatedDegrees:(CGFloat)degrees anchorPoint
17901781
return camera;
17911782
}
17921783

1793-
- (MGLMapCamera *)currentCameraWithEstimatedPitch:(CGFloat)pitch anchorPoint:(CGPoint)anchorPoint
1784+
- (MGLMapCamera *)cameraByTiltingToPitch:(CGFloat)pitch
17941785
{
17951786
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
17961787
mbgl::CameraOptions currentCameraOptions = _mbglMap->getCameraOptions(padding);
17971788

17981789
MGLMapCamera *camera;
1799-
1800-
mbgl::ScreenCoordinate anchor = mbgl::ScreenCoordinate { anchorPoint.x, anchorPoint.y };
1790+
18011791
currentCameraOptions.pitch = pitch * mbgl::util::DEG2RAD;
1802-
currentCameraOptions.anchor = anchor;
18031792
camera = [self cameraForCameraOptions:currentCameraOptions];
18041793

18051794
return camera;
18061795
}
18071796

1808-
- (double)currentCameraZoom
1809-
{
1810-
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
1811-
mbgl::CameraOptions currentCameraOptions = _mbglMap->getCameraOptions(padding);
1812-
double zoom = 0.0;
1813-
1814-
if (currentCameraOptions.zoom) {
1815-
zoom = *currentCameraOptions.zoom;
1816-
}
1817-
1818-
return zoom;
1819-
}
1820-
18211797
- (CGPoint)anchorPointForGesture:(UIGestureRecognizer *)gesture {
18221798
if (self.userTrackingMode != MGLUserTrackingModeNone)
18231799
{

0 commit comments

Comments
 (0)