From df287d3dfcd962432d02eecbe5dd14d4986b46f0 Mon Sep 17 00:00:00 2001 From: Jason Wray Date: Fri, 4 May 2018 14:45:29 -0400 Subject: [ios] Fix MGLAnnotationView.rotatesToMatchCamera clobbering other transforms --- platform/ios/CHANGELOG.md | 1 + platform/ios/src/MGLAnnotationView.mm | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index d979021760..2c535448d0 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -14,6 +14,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT * Fixed an issue where selecting an onscreen annotation could move the map unintentionally. ([#11731](https://github.com/mapbox/mapbox-gl-native/pull/11731)) * Fixed an issue where annotation views could become distorted if `rotatesToMatchCamera` is `YES`. ([#11817](https://github.com/mapbox/mapbox-gl-native/pull/11817)) +* Fixed `MGLAnnotationView.rotatesToMatchCamera` overriding other transforms that might be applied to annotation views that had this property enabled. ([#11842](https://github.com/mapbox/mapbox-gl-native/pull/11842)) ### Other changes diff --git a/platform/ios/src/MGLAnnotationView.mm b/platform/ios/src/MGLAnnotationView.mm index 1c53ba507a..46b0f56a79 100644 --- a/platform/ios/src/MGLAnnotationView.mm +++ b/platform/ios/src/MGLAnnotationView.mm @@ -11,8 +11,9 @@ @property (nonatomic, readwrite, nullable) NSString *reuseIdentifier; @property (nonatomic, readwrite) CATransform3D lastAppliedScaleTransform; -@property (nonatomic, readwrite) CATransform3D lastAppliedRotateTransform; @property (nonatomic, readwrite) CGFloat lastPitch; +@property (nonatomic, readwrite) CATransform3D lastAppliedRotationTransform; +@property (nonatomic, readwrite) CGFloat lastDirection; @property (nonatomic, weak) UIPanGestureRecognizer *panGestureRecognizer; @property (nonatomic, weak) UILongPressGestureRecognizer *longPressRecognizer; @property (nonatomic, weak) MGLMapView *mapView; @@ -43,9 +44,9 @@ - (void)commonInitWithAnnotation:(nullable id)annotation reuseIdentifier:(nullable NSString *)reuseIdentifier { _lastAppliedScaleTransform = CATransform3DIdentity; + _lastAppliedRotationTransform = CATransform3DIdentity; _annotation = annotation; _reuseIdentifier = [reuseIdentifier copy]; - _scalesWithViewingDistance = NO; _enabled = YES; } @@ -159,7 +160,7 @@ // We keep track of each viewing distance scale transform that we apply. Each iteration, // we can account for it so that we don't get cumulative scaling every time we move. - // We also avoid clobbering any existing transform passed in by the client, too. + // We also avoid clobbering any existing transform passed in by the client or this SDK. CATransform3D undoOfLastScaleTransform = CATransform3DInvert(_lastAppliedScaleTransform); CATransform3D newScaleTransform = CATransform3DMakeScale(pitchAdjustedScale, pitchAdjustedScale, 1); CATransform3D effectiveTransform = CATransform3DConcat(undoOfLastScaleTransform, newScaleTransform); @@ -181,11 +182,20 @@ { if (self.rotatesToMatchCamera == NO) return; - CGFloat directionRad = self.mapView.direction * M_PI / 180.0; - CATransform3D newRotateTransform = CATransform3DMakeRotation(-directionRad, 0, 0, 1); - self.layer.transform = CATransform3DConcat(CATransform3DIdentity, newRotateTransform); - - _lastAppliedRotateTransform = newRotateTransform; + CGFloat direction = -MGLRadiansFromDegrees(self.mapView.direction); + + // Return early if the map view has the same rotation as the already-applied transform. + if (direction == _lastDirection) return; + _lastDirection = direction; + + // We keep track of each rotation transform that we apply. Each iteration, + // we can account for it so that we don't get cumulative rotation every time we move. + // We also avoid clobbering any existing transform passed in by the client or this SDK. + CATransform3D undoOfLastRotationTransform = CATransform3DInvert(_lastAppliedRotationTransform); + CATransform3D newRotationTransform = CATransform3DMakeRotation(direction, 0, 0, 1); + CATransform3D effectiveTransform = CATransform3DConcat(undoOfLastRotationTransform, newRotationTransform); + self.layer.transform = CATransform3DConcat(self.layer.transform, effectiveTransform); + _lastAppliedRotationTransform = newRotationTransform; } #pragma mark - Draggable -- cgit v1.2.1