-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Make annotation view rotation alignment configurable #8308
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,19 @@ typedef NS_ENUM(NSUInteger, MGLAnnotationViewDragState) { | |
*/ | ||
@property (nonatomic, assign) BOOL scalesWithViewingDistance; | ||
|
||
/** | ||
A Boolean value that determines whether the annotation view rotates together | ||
with the mapview. | ||
|
||
When the value of this property is `YES` and the map is rotated, the annotation | ||
view rotates. This is also the behavior of `MGLAnnotationImage` objects. When the | ||
value of this property is `NO` the annotation has its rotation angle fixed. | ||
|
||
The default value of this property is `NO`. Set this property to `YES` if the | ||
view’s rotation is important. | ||
*/ | ||
@property (nonatomic, assign) BOOL rotatesWithMap; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #5245 at one point called this property |
||
|
||
#pragma mark Managing the Selection State | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ @interface MGLAnnotationView () <UIGestureRecognizerDelegate> | |
|
||
@property (nonatomic, readwrite, nullable) NSString *reuseIdentifier; | ||
@property (nonatomic, readwrite) CATransform3D lastAppliedScaleTransform; | ||
@property (nonatomic, readwrite) CATransform3D lastAppliedRotateTransform; | ||
@property (nonatomic, weak) UIPanGestureRecognizer *panGestureRecognizer; | ||
@property (nonatomic, weak) UILongPressGestureRecognizer *longPressRecognizer; | ||
@property (nonatomic, weak) MGLMapView *mapView; | ||
|
@@ -25,8 +26,10 @@ - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier | |
if (self) | ||
{ | ||
_lastAppliedScaleTransform = CATransform3DIdentity; | ||
_lastAppliedRotateTransform = CATransform3DIdentity; | ||
_reuseIdentifier = [reuseIdentifier copy]; | ||
_scalesWithViewingDistance = YES; | ||
_rotatesWithMap = NO; | ||
_enabled = YES; | ||
} | ||
return self; | ||
|
@@ -42,6 +45,7 @@ - (instancetype)initWithCoder:(NSCoder *)decoder { | |
_annotation = [decoder decodeObjectOfClass:[NSObject class] forKey:@"annotation"]; | ||
_centerOffset = [decoder decodeCGVectorForKey:@"centerOffset"]; | ||
_scalesWithViewingDistance = [decoder decodeBoolForKey:@"scalesWithViewingDistance"]; | ||
_rotatesWithMap = [decoder decodeBoolForKey:@"rotatesWithMap"]; | ||
_selected = [decoder decodeBoolForKey:@"selected"]; | ||
_enabled = [decoder decodeBoolForKey:@"enabled"]; | ||
self.draggable = [decoder decodeBoolForKey:@"draggable"]; | ||
|
@@ -55,6 +59,7 @@ - (void)encodeWithCoder:(NSCoder *)coder { | |
[coder encodeObject:_annotation forKey:@"annotation"]; | ||
[coder encodeCGVector:_centerOffset forKey:@"centerOffset"]; | ||
[coder encodeBool:_scalesWithViewingDistance forKey:@"scalesWithViewingDistance"]; | ||
[coder encodeBool:_rotatesWithMap forKey:@"rotatesWithMap"]; | ||
[coder encodeBool:_selected forKey:@"selected"]; | ||
[coder encodeBool:_enabled forKey:@"enabled"]; | ||
[coder encodeBool:_draggable forKey:@"draggable"]; | ||
|
@@ -146,6 +151,29 @@ - (void)updateScaleTransformForViewingDistance | |
} | ||
} | ||
|
||
- (void)setRotatesWithMap:(BOOL)rotatesWithMap | ||
{ | ||
if (_rotatesWithMap != rotatesWithMap) | ||
{ | ||
_rotatesWithMap = rotatesWithMap; | ||
[self updateRotateTransform]; | ||
} | ||
} | ||
|
||
- (void)updateRotateTransform | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can tell, this method only gets called when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. I was copy-pasting the code from my private repository that was used with the project. Forgot to add the call to |
||
{ | ||
if (self.rotatesWithMap == NO) return; | ||
|
||
CATransform3D undoOfLastRotateTransform = CATransform3DInvert(_lastAppliedRotateTransform); | ||
|
||
CGFloat directionRad = self.mapView.direction * M_PI / 180.0; | ||
CATransform3D newRotateTransform = CATransform3DMakeRotation(-directionRad, 0, 0, 1); | ||
CATransform3D effectiveTransform = CATransform3DConcat(undoOfLastRotateTransform, newRotateTransform); | ||
|
||
self.layer.transform = CATransform3DConcat(self.layer.transform, effectiveTransform); | ||
_lastAppliedRotateTransform = newRotateTransform; | ||
} | ||
|
||
#pragma mark - Draggable | ||
|
||
- (void)setDraggable:(BOOL)draggable | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: replace “mapview” with “map”, since the
direction
property reflects the rotation of the content within the map view, not the map view itself.