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

Commit

Permalink
[ios] Made annotation view position animatable
Browse files Browse the repository at this point in the history
We don’t normally want an annotation view to animate its position, because that makes the view appear to lag behind the map. But when the annotation view moves due to the underlying annotation model object moving, the developer may want exactly that effect.

This change continues to disable the default implicit bounds (and now position) animation. It also groups the view updates in -updateAnnotationViews in a transaction that disables animation actions, to improve perceived performance with a large number of annotations. However, when the annotation model object changes, we move the annotation view outside of that transaction to allow the developer to opt into animation.

If the developer moreover wants the annotation view to animate even due to the viewport changing, they can override -setCenter: to use a UIView animation block.

Fixes #5230.
  • Loading branch information
1ec5 committed Jun 24, 2016
1 parent 995a19b commit a19e6b7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 12 additions & 0 deletions platform/ios/app/MBXAnnotationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ - (void)setCenterColor:(UIColor *)centerColor {
}
}

- (nullable id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
{
if ([event isEqualToString:@"position"])
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:event];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.speed = 0.1;
return animation;
}
return [super actionForLayer:layer forKey:event];
}

@end
2 changes: 1 addition & 1 deletion platform/ios/src/MGLAnnotationView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ - (void)updateScaleForPitch:(CGFloat)pitch
- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
{
// Allow mbgl to drive animation of this view’s bounds.
if ([event isEqualToString:@"bounds"])
if ([event isEqualToString:@"bounds"] || [event isEqualToString:@"position"])
{
return [NSNull null];
}
Expand Down
14 changes: 13 additions & 1 deletion platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1774,12 +1774,19 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag);
NSString *symbolName;
if (!annotationContext.annotationView)
if (annotationContext.annotationView)
{
// Redundantly move the associated annotation view outside the scope of the animation-less transaction block in -updateAnnotationViews.
CGPoint center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self];
[annotationContext.annotationView setCenter:center pitch:self.camera.pitch];
}
else
{
MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag];
symbolName = annotationImage.styleIconIdentifier;
}

// Update the annotation’s backing geometry to match the annotation model object. Any associated annotation view is also moved by side effect. However, -updateAnnotationViews disables the view’s animation actions, because it can’t distinguish between moves due to the viewport changing and moves due to the annotation’s coordinate changing.
_mbglMap->updateAnnotation(annotationTag, mbgl::SymbolAnnotation { point, symbolName.UTF8String ?: "" });
if (annotationTag == _selectedAnnotationTag)
{
Expand Down Expand Up @@ -4470,6 +4477,9 @@ - (void)updateAnnotationViews
return;
}

[CATransaction begin];
[CATransaction setDisableActions:YES];

for (auto &pair : _annotationContextsByAnnotationTag)
{
CGRect viewPort = CGRectInset(self.bounds, -_largestAnnotationViewSize.width - MGLAnnotationUpdateViewportOutset.width, -_largestAnnotationViewSize.height - MGLAnnotationUpdateViewportOutset.width);
Expand Down Expand Up @@ -4506,6 +4516,8 @@ - (void)updateAnnotationViews
[annotationView setCenter:center pitch:self.camera.pitch];
}
}

[CATransaction commit];
}

- (void)enqueueAnnotationViewForAnnotationContext:(MGLAnnotationContext &)annotationContext
Expand Down

0 comments on commit a19e6b7

Please sign in to comment.