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

Commit 2501b2c

Browse files
committed
[ios] preserve annotation transforms during perspective scaling
1 parent f07f592 commit 2501b2c

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

platform/ios/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT
3939
* To make an MGLPolyline or MGLPolygon span the antimeridian, specify coordinates with longitudes greater than 180° or less than −180°. ([#6088](https://github.com/mapbox/mapbox-gl-native/pull/6088))
4040
* Improved the performance of relocating a non-view-backed point annotation by changing its `coordinate` property. ([#5385](https://github.com/mapbox/mapbox-gl-native/pull/5385))
4141
* Improved the precision of annotations at zoom levels greater than 18. ([#5517](https://github.com/mapbox/mapbox-gl-native/pull/5517))
42+
* Fixed an issue that could reset user-added transformations on annotation views. ([#6166](https://github.com/mapbox/mapbox-gl-native/pull/6166))
4243

4344
### Other changes
4445

platform/ios/src/MGLAnnotationView.mm

+16-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ @interface MGLAnnotationView () <UIGestureRecognizerDelegate>
1111

1212
@property (nonatomic, readwrite, nullable) NSString *reuseIdentifier;
1313
@property (nonatomic, readwrite, nullable) id <MGLAnnotation> annotation;
14+
@property (nonatomic, readwrite) CATransform3D lastAppliedScaleTransform;
1415
@property (nonatomic, weak) UIPanGestureRecognizer *panGestureRecognizer;
1516
@property (nonatomic, weak) UILongPressGestureRecognizer *longPressRecognizer;
1617
@property (nonatomic, weak) MGLMapView *mapView;
@@ -24,6 +25,7 @@ - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
2425
self = [self initWithFrame:CGRectZero];
2526
if (self)
2627
{
28+
_lastAppliedScaleTransform = CATransform3DIdentity;
2729
_reuseIdentifier = [reuseIdentifier copy];
2830
_scalesWithViewingDistance = YES;
2931
_enabled = YES;
@@ -68,32 +70,22 @@ - (void)setCenter:(CGPoint)center
6870
center.y += _centerOffset.dy;
6971

7072
super.center = center;
71-
[self updateTransform];
73+
[self updateScaleTransformForViewingDistance];
7274
}
7375

7476
- (void)setScalesWithViewingDistance:(BOOL)scalesWithViewingDistance
7577
{
7678
if (_scalesWithViewingDistance != scalesWithViewingDistance)
7779
{
7880
_scalesWithViewingDistance = scalesWithViewingDistance;
79-
[self updateTransform];
81+
[self updateScaleTransformForViewingDistance];
8082
}
8183
}
8284

83-
- (void)updateTransform
85+
- (void)updateScaleTransformForViewingDistance
8486
{
85-
// Omit applying a new transformation while the view is being dragged.
86-
if (self.dragState == MGLAnnotationViewDragStateDragging)
87-
{
88-
return;
89-
}
90-
91-
self.layer.transform = CATransform3DIdentity;
92-
if ( ! self.scalesWithViewingDistance)
93-
{
94-
return;
95-
}
96-
87+
if (self.scalesWithViewingDistance == NO || self.dragState == MGLAnnotationViewDragStateDragging) return;
88+
9789
CGFloat superviewHeight = CGRectGetHeight(self.superview.frame);
9890
if (superviewHeight > 0.0) {
9991
// Find the maximum amount of scale reduction to apply as the view's center moves from the top
@@ -115,9 +107,15 @@ - (void)updateTransform
115107
// map view is 50% pitched then the annotation view should be reduced by 37.5% (.75 * .5). The
116108
// reduction is then normalized for a scale of 1.0.
117109
CGFloat pitchAdjustedScale = 1.0 - maxScaleReduction * pitchIntensity;
118-
119-
CATransform3D transform = CATransform3DIdentity;
120-
self.layer.transform = CATransform3DScale(transform, pitchAdjustedScale, pitchAdjustedScale, 1);
110+
111+
// We keep track of each viewing distance scale transform that we apply. Each iteration,
112+
// we can account for it so that we don't get cumulative scaling every time we move.
113+
// We also avoid clobbering any existing transform passed in by the client, too.
114+
CATransform3D undoOfLastScaleTransform = CATransform3DInvert(_lastAppliedScaleTransform);
115+
CATransform3D newScaleTransform = CATransform3DMakeScale(pitchAdjustedScale, pitchAdjustedScale, 1);
116+
CATransform3D effectiveTransform = CATransform3DConcat(undoOfLastScaleTransform, newScaleTransform);
117+
self.layer.transform = CATransform3DConcat(self.layer.transform, effectiveTransform);
118+
_lastAppliedScaleTransform = newScaleTransform;
121119
}
122120
}
123121

0 commit comments

Comments
 (0)