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

Turn the compass into a button #1830

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/mbgl/ios/MGLMapView.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ IB_DESIGNABLE
* The default value of this property is `YES`. */
@property(nonatomic, getter=isRotateEnabled) BOOL rotateEnabled;

/** The compass image view shown in the upper-right when the map is rotated. */
@property (nonatomic, readonly) UIImageView *compassView;
/** The compass button shown in the upper-right when the map is rotated. */
@property (nonatomic, readonly) UIButton *compassButton;

/** The Mapbox logo image view shown in the lower-left of the map. */
@property (nonatomic, readonly) UIImageView *logoView;
Expand Down
39 changes: 21 additions & 18 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ @interface MGLMapView () <UIGestureRecognizerDelegate, GLKViewDelegate, CLLocati
@property (nonatomic) GLKView *glView;
@property (nonatomic) UIImageView *glSnapshotView;
@property (nonatomic) NSOperationQueue *regionChangeDelegateQueue;
@property (nonatomic, readwrite) UIImageView *compassView;
@property (nonatomic, readwrite) UIButton *compassButton;
@property (nonatomic, readwrite) UIImageView *logoView;
@property (nonatomic) NS_MUTABLE_ARRAY_OF(NSLayoutConstraint *) *logoViewConstraints;
@property (nonatomic, readwrite) UIButton *attributionButton;
Expand Down Expand Up @@ -314,14 +314,16 @@ - (void)commonInit

// setup compass
//
_compassView = [[UIImageView alloc] initWithImage:[MGLMapView resourceImageNamed:@"Compass.png"]];
_compassView.accessibilityLabel = @"Compass";
_compassView.frame = CGRectMake(0, 0, _compassView.image.size.width, _compassView.image.size.height);
_compassView.alpha = 0;
_compassButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *compassImage = [MGLMapView resourceImageNamed:@"Compass.png"];
[_compassButton setImage:compassImage forState:UIControlStateNormal];
_compassButton.accessibilityLabel = @"Compass";
_compassButton.frame = CGRectMake(0, 0, compassImage.size.width, compassImage.size.height);
_compassButton.alpha = 0;
[_compassButton addTarget:self action:@selector(resetNorthWithCompass:) forControlEvents:UIControlEventTouchUpInside];
UIView *container = [[UIView alloc] initWithFrame:CGRectZero];
[container addSubview:_compassView];
[container addSubview:_compassButton];
container.translatesAutoresizingMaskIntoConstraints = NO;
[container addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCompassTapGesture:)]];
[self addSubview:container];

// setup interaction
Expand Down Expand Up @@ -499,7 +501,7 @@ - (void)updateConstraints

// compass
//
UIView *compassContainer = self.compassView.superview;
UIView *compassContainer = self.compassButton.superview;
if ([NSLayoutConstraint respondsToSelector:@selector(deactivateConstraints:)])
{
[NSLayoutConstraint deactivateConstraints:compassContainer.constraints];
Expand Down Expand Up @@ -539,14 +541,15 @@ - (void)updateConstraints
multiplier:1
constant:5]];

UIImage *compassImage = [self.compassButton imageForState:UIControlStateNormal];
[compassContainerConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:self.compassView.image.size.width]];
constant:compassImage.size.width]];

[compassContainerConstraints addObject:
[NSLayoutConstraint constraintWithItem:compassContainer
Expand All @@ -555,7 +558,7 @@ - (void)updateConstraints
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:self.compassView.image.size.height]];
constant:compassImage.size.height]];
if ([NSLayoutConstraint respondsToSelector:@selector(activateConstraints:)])
{
[NSLayoutConstraint activateConstraints:compassContainerConstraints];
Expand Down Expand Up @@ -700,7 +703,7 @@ - (void)layoutSubviews
[self.attributionSheet dismissWithClickedButtonIndex:self.attributionSheet.cancelButtonIndex animated:YES];
}

if (self.compassView.alpha)
if (self.compassButton.alpha)
{
[self updateHeadingForDeviceOrientation];
[self updateCompass];
Expand Down Expand Up @@ -789,7 +792,7 @@ - (void)updateTintColorForView:(UIView *)view

#pragma mark - Gestures -

- (void)handleCompassTapGesture:(__unused id)sender
- (void)resetNorthWithCompass:(__unused id)sender
{
[self resetNorthAnimated:YES];

Expand Down Expand Up @@ -1403,7 +1406,7 @@ - (void)resetNorthAnimated:(BOOL)animated
[UIView animateWithDuration:duration
animations:^
{
self.compassView.transform = CGAffineTransformIdentity;
self.compassButton.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished)
{
Expand Down Expand Up @@ -2628,27 +2631,27 @@ - (void)updateCompass
while (degrees >= 360) degrees -= 360;
while (degrees < 0) degrees += 360;

self.compassView.transform = CGAffineTransformMakeRotation(MGLRadiansFromDegrees(degrees));
self.compassButton.transform = CGAffineTransformMakeRotation(MGLRadiansFromDegrees(degrees));

if (_mbglMap->getBearing() && self.compassView.alpha < 1)
if (_mbglMap->getBearing() && self.compassButton.alpha < 1)
{
[UIView animateWithDuration:MGLAnimationDuration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^
{
self.compassView.alpha = 1;
self.compassButton.alpha = 1;
}
completion:nil];
}
else if (_mbglMap->getBearing() == 0 && self.compassView.alpha > 0)
else if (_mbglMap->getBearing() == 0 && self.compassButton.alpha > 0)
{
[UIView animateWithDuration:MGLAnimationDuration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^
{
self.compassView.alpha = 0;
self.compassButton.alpha = 0;
}
completion:nil];
}
Expand Down