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

Commit

Permalink
[ios] code refactoring to clarify the conditions to execute a gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-guerra committed Feb 9, 2017
1 parent cb1e02b commit e6cec13
Showing 1 changed file with 38 additions and 60 deletions.
98 changes: 38 additions & 60 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,6 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
_mbglMap->cancelTransitions();

MGLMapCamera *oldCamera = self.camera;
MGLMapCamera *toCamera;

if (pan.state == UIGestureRecognizerStateBegan)
{
Expand All @@ -1220,13 +1219,12 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
{
CGPoint delta = [pan translationInView:pan.view];

toCamera = [self cameraByPanningWithTranslation:delta panGesture:pan];
MGLMapCamera *toCamera = [self cameraByPanningWithTranslation:delta panGesture:pan];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {
_mbglMap->moveBy({ delta.x, delta.y });
[pan setTranslation:CGPointZero inView:pan.view];
}
Expand All @@ -1246,13 +1244,12 @@ - (void)handlePanGesture:(UIPanGestureRecognizer *)pan
if (drift)
{
CGPoint offset = CGPointMake(velocity.x * self.decelerationRate / 4, velocity.y * self.decelerationRate / 4);
toCamera = [self cameraByPanningWithTranslation:offset panGesture:pan];
MGLMapCamera *toCamera = [self cameraByPanningWithTranslation:offset panGesture:pan];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {
_mbglMap->moveBy({ offset.x, offset.y }, MGLDurationInSecondsFromTimeInterval(self.decelerationRate));
}
}
Expand Down Expand Up @@ -1295,19 +1292,16 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
else if (pinch.state == UIGestureRecognizerStateChanged)
{
CGFloat newScale = self.scale * pinch.scale;

if (log2(newScale) < _mbglMap->getMinZoom()) return;
double zoom = log2(newScale);
if (zoom < _mbglMap->getMinZoom()) return;

// Calculates the final camera zoom, has no effect within current map camera.
MGLMapCamera *toCamera;
double zoom = log2(newScale);
toCamera = [self cameraByZoomingToZoomLevel:zoom aroundAnchorPoint:centerPoint];
MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:zoom aroundAnchorPoint:centerPoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {
_mbglMap->setScale(newScale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
}
// The gesture recognizer only reports the gesture’s current center
Expand Down Expand Up @@ -1356,14 +1350,12 @@ - (void)handlePinchGesture:(UIPinchGestureRecognizer *)pinch
BOOL drift = velocity && duration;

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

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
{
self.camera = oldCamera;
drift = NO;
} else {
if (drift)
Expand All @@ -1388,8 +1380,6 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate

CGPoint centerPoint = [self anchorPointForGesture:rotate];
MGLMapCamera *oldCamera = self.camera;
MGLMapCamera *toCamera;


if (rotate.state == UIGestureRecognizerStateBegan)
{
Expand All @@ -1416,14 +1406,13 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
newDegrees = fmaxf(newDegrees, -30);
}

toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];
MGLMapCamera *toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {
_mbglMap->setBearing(newDegrees, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
_mbglMap->setBearing(newDegrees, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
}

[self notifyMapChange:mbgl::MapChangeRegionIsChanging];
Expand All @@ -1439,13 +1428,12 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
CGFloat newRadians = radians + velocity * decelerationRate * 0.1;
CGFloat newDegrees = MGLDegreesFromRadians(newRadians) * -1;

toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];
MGLMapCamera *toCamera = [self cameraByRotatingToDirection:newDegrees aroundAnchorPoint:centerPoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {
_mbglMap->setBearing(newDegrees, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y }, MGLDurationInSecondsFromTimeInterval(decelerationRate));

[self notifyGestureDidEndWithDrift:YES];
Expand All @@ -1457,7 +1445,6 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
[weakSelf unrotateIfNeededForGesture];
}];
}

}
else
{
Expand Down Expand Up @@ -1575,17 +1562,15 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
{
MGLMapCamera *oldCamera = self.camera;

double zoom = self.zoomLevel;
double newZoom = zoom + 1.0;
double newZoom = self.zoomLevel + 1.0;
CGPoint gesturePoint = [self anchorPointForGesture:doubleTap];

MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {
[self trackGestureEvent:MGLEventGestureDoubleTap forRecognizer:doubleTap];

mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
Expand All @@ -1598,7 +1583,6 @@ - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
[weakSelf unrotateIfNeededForGesture];
}];
}

}
}

Expand All @@ -1624,12 +1608,10 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap

MGLMapCamera *toCamera = [self cameraByZoomingToZoomLevel:newZoom aroundAnchorPoint:gesturePoint];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {

mbgl::ScreenCoordinate center(gesturePoint.x, gesturePoint.y);
_mbglMap->scaleBy(0.5, center, MGLDurationInSecondsFromTimeInterval(MGLAnimationDuration));

Expand All @@ -1640,7 +1622,6 @@ - (void)handleTwoFingerTapGesture:(UITapGestureRecognizer *)twoFingerTap
[weakSelf unrotateIfNeededForGesture];
}];
}

}
}

Expand Down Expand Up @@ -1679,11 +1660,10 @@ - (void)handleQuickZoomGesture:(UILongPressGestureRecognizer *)quickZoom

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

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {
_mbglMap->scaleBy(scale, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
}

Expand All @@ -1702,7 +1682,6 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag

_mbglMap->cancelTransitions();
MGLMapCamera *oldCamera = self.camera;
MGLMapCamera *toCamera;

if (twoFingerDrag.state == UIGestureRecognizerStateBegan)
{
Expand All @@ -1719,13 +1698,12 @@ - (void)handleTwoFingerDragGesture:(UIPanGestureRecognizer *)twoFingerDrag

CGPoint centerPoint = [self anchorPointForGesture:twoFingerDrag];

toCamera = [self cameraByTiltingToPitch:pitchNew];
MGLMapCamera *toCamera = [self cameraByTiltingToPitch:pitchNew];

if ([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& ![self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera])
if (![self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)] ||
([self.delegate respondsToSelector:@selector(mapView:shouldChangeFromCamera:toCamera:)]
&& [self.delegate mapView:self shouldChangeFromCamera:oldCamera toCamera:toCamera]))
{
self.camera = oldCamera;
} else {
_mbglMap->setPitch(pitchNew, mbgl::ScreenCoordinate { centerPoint.x, centerPoint.y });
}

Expand Down

0 comments on commit e6cec13

Please sign in to comment.