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

Commit

Permalink
Add compass image as a sublayer (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Rex authored Oct 12, 2020
1 parent 5970296 commit 97840a6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
4 changes: 2 additions & 2 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ workflows:
- ios-build:
matrix:
parameters:
xcode: ["11.1.0", "11.7.0"]
xcode: ["11.7.0", "12.0.0"]
buildtype: ["Debug", "Release"]
- ios-release-template:
xcode: "12.0.0"
Expand Down Expand Up @@ -57,7 +57,7 @@ workflows:
- ios-build:
matrix:
parameters:
xcode: ["11.3.1", "11.5.0", "11.6.0", "12.0.0"]
xcode: ["11.1.0", "11.3.1", "12.2.0"]
buildtype: ["Debug", "Release"]
- ios-sanitize-nightly:
requires:
Expand Down
11 changes: 8 additions & 3 deletions platform/ios/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT

## 6.3.0

### 🐞 Bug fixes

* Partly fixed an issue on iOS 14 where `-[UIView layoutSubviews]` was being repeatedly called. ([#501](https://github.com/mapbox/mapbox-gl-native-ios/pull/501))
* Fixed a bug with UIViews being incorrectly updated with a one frame delay. ([#483](https://github.com/mapbox/mapbox-gl-native-ios/pull/483))
* Fixed an issue where CocoaPods users could not install the SDK when using Xcode 12. ([#482](https://github.com/mapbox/mapbox-gl-native-ios/pull/482))

### 🔧 Dependencies

* Updated `mapbox-events-ios` to `0.10.5-beta.2` in order to add additional iOS 14 support.([#491](https://github.com/mapbox/mapbox-gl-native-ios/pull/491))

### 🐞 Bug fixes
### ✨ Other changes

* Fixed a bug with UIViews being incorrectly updated with a one frame delay. ([#483](https://github.com/mapbox/mapbox-gl-native-ios/pull/483))
* Fixed an issue where CocoaPods users could not install the SDK when using Xcode 12. ([#482](https://github.com/mapbox/mapbox-gl-native-ios/pull/482))
* The default branch is now `main`. Please rebase any existing branches you may have. ([#489](https://github.com/mapbox/mapbox-gl-native-ios/pull/489))

## 6.2.1 - September 23, 2020

Expand Down
24 changes: 20 additions & 4 deletions platform/ios/src/MGLCompassButton.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@interface MGLCompassButton ()

@property (nonatomic, weak) MGLMapView *mapView;
@property (nonatomic) CALayer *imageLayer;
@property (nonatomic) MGLCompassDirectionFormatter *accessibilityCompassFormatter;

@end
Expand All @@ -30,8 +31,21 @@ - (instancetype)initWithMapView:(MGLMapView *)mapView {
return self;
}

- (void)setImage:(UIImage *)image {
self.imageLayer.contents = (id)image.CGImage;
}

- (void)commonInit {
self.image = self.compassImage;
UIImage *image = self.compassImage;
CGRect bounds = (CGRect){CGPointZero, image.size};
self.bounds = bounds;

self.imageLayer = [[CALayer alloc] init];
self.imageLayer.frame = bounds;
self.imageLayer.contentsGravity = kCAGravityResizeAspect;

self.image = image;
[self.layer addSublayer:self.imageLayer];

self.compassVisibility = MGLOrnamentVisibilityAdaptive;

Expand All @@ -48,8 +62,6 @@ - (void)commonInit {

self.accessibilityCompassFormatter = [[MGLCompassDirectionFormatter alloc] init];
self.accessibilityCompassFormatter.unitStyle = NSFormattingUnitStyleLong;

[self sizeToFit];
}

- (void)setCompassVisibility:(MGLOrnamentVisibility)compassVisibility {
Expand Down Expand Up @@ -96,7 +108,11 @@ - (void)updateCompass {
- (void)updateCompassAnimated:(BOOL)animated {
CLLocationDirection direction = self.mapView.direction;
CLLocationDirection plateDirection = mbgl::util::wrap(-direction, 0., 360.);
self.transform = CGAffineTransformMakeRotation(MGLRadiansFromDegrees(plateDirection));

[CATransaction begin];
[CATransaction setValue:@(YES) forKey:kCATransactionDisableActions];
self.imageLayer.transform = CATransform3DMakeRotation(MGLRadiansFromDegrees(plateDirection), 0.0, 0.0, 1.0);
[CATransaction commit];

self.isAccessibilityElement = direction > 0;
self.accessibilityValue = [self.accessibilityCompassFormatter stringFromDirection:direction];
Expand Down
1 change: 1 addition & 0 deletions platform/ios/src/MGLCompassButton_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (instancetype)compassButtonWithMapView:(MGLMapView *)mapView;

@property (nonatomic, weak) MGLMapView *mapView;
@property (nonatomic) CALayer *imageLayer;

- (void)updateCompass;

Expand Down
5 changes: 4 additions & 1 deletion platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7269,7 +7269,10 @@ - (CGPoint)userLocationAnnotationViewCenter

- (void)updateCompass
{
[self.compassView updateCompass];
if ((self.compassView.compassVisibility != MGLOrnamentVisibilityHidden) &&
(!self.compassView.isHidden)) {
[self.compassView updateCompass];
}
}

- (void)updateScaleBar
Expand Down
19 changes: 13 additions & 6 deletions platform/ios/test/MGLMapViewCompassViewTests.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import <Mapbox/Mapbox.h>
#import <XCTest/XCTest.h>

#import "MGLCompassButton_Private.h"
#import <mbgl/math/wrap.hpp>

@interface MGLMapView (MGLCompassButtonTests)
Expand Down Expand Up @@ -84,14 +84,21 @@ - (void)testCompassRotation {
for (NSNumber *degrees in @[@-999, @-359, @-240, @-180, @-90, @-45, @0, @45, @90, @180, @240, @360, @999]) {
self.mapView.direction = [degrees doubleValue];
CGFloat wrappedDirection = mbgl::util::wrap(-self.mapView.direction, 0., 360.);
CGAffineTransform rotation = CGAffineTransformMakeRotation(MGLRadiansFromDegrees(wrappedDirection));
XCTAssertTrue(CGAffineTransformEqualToTransform(self.mapView.compassView.transform, rotation),
@"Compass transform direction %f° should equal wrapped transform direction %f° (~%.f°).", [self degreesFromAffineTransform:self.mapView.compassView.transform], [self degreesFromAffineTransform:rotation], wrappedDirection);
CATransform3D rotation = CATransform3DMakeRotation(MGLRadiansFromDegrees(wrappedDirection), 0.0, 0.0, 1.0);

CGFloat deg1 = [self degreesFromAffineTransform:self.mapView.compassView.imageLayer.transform];
CGFloat deg2 = [self degreesFromAffineTransform:rotation];
XCTAssertEqual(deg1, deg2);
XCTAssertTrue(CATransform3DEqualToTransform(self.mapView.compassView.imageLayer.transform, rotation),
@"Compass transform direction %f° should equal wrapped transform direction %f° (~%.f°).",
deg1,
deg2,
wrappedDirection);
}
}

- (CGFloat)degreesFromAffineTransform:(CGAffineTransform)transform {
CGFloat angle = atan2f(transform.b, transform.a);
- (CGFloat)degreesFromAffineTransform:(CATransform3D)transform {
CGFloat angle = atan2f(transform.m12, transform.m11);
return MGLDegreesFromRadians(angle);
}

Expand Down

0 comments on commit 97840a6

Please sign in to comment.