From 2b7039b0ebd0a3572917435ddbfb715353d7b562 Mon Sep 17 00:00:00 2001 From: Fredrik Karlsson Date: Wed, 18 May 2016 12:22:04 -0700 Subject: [PATCH 1/4] [ios] wip flatten annotation view --- platform/ios/Mapbox.playground/Contents.swift | 24 +++++++++++++++ platform/ios/app/MBXViewController.m | 1 + platform/ios/src/MGLAnnotationView.h | 10 +++++++ platform/ios/src/MGLAnnotationView.m | 29 +++++++++++++++++-- platform/ios/src/MGLAnnotationView_Private.h | 1 + 5 files changed, 63 insertions(+), 2 deletions(-) diff --git a/platform/ios/Mapbox.playground/Contents.swift b/platform/ios/Mapbox.playground/Contents.swift index 514cb10160f..5b96ef47d4f 100644 --- a/platform/ios/Mapbox.playground/Contents.swift +++ b/platform/ios/Mapbox.playground/Contents.swift @@ -5,17 +5,30 @@ import Mapbox let width: CGFloat = 700 let height: CGFloat = 800 +class Responder: NSObject { + var mapView: MGLMapView? + func togglePitch(sender: UISwitch) { + let camera = mapView!.camera + camera.pitch = sender.on ? 60 : 0 + mapView!.setCamera(camera, animated: false) + } +} + //: A control panel let panelWidth: CGFloat = 200 let panel = UIView(frame: CGRect(x: width - panelWidth, y: 0, width: 200, height: 100)) panel.alpha = 0.8 panel.backgroundColor = UIColor.whiteColor() + +// Delete markers let deleteSwitchLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 30)) deleteSwitchLabel.adjustsFontSizeToFitWidth = true deleteSwitchLabel.text = "Delete Markers" let deleteMarkerSwitchView = UISwitch(frame: CGRect(x: panelWidth - panelWidth / 2.0, y:0, width: 100, height: 50)) panel.addSubview(deleteSwitchLabel) panel.addSubview(deleteMarkerSwitchView) + +// Hide markers let hideSwitchLabel = UILabel(frame: CGRect(x: 0, y: 30, width: 100, height: 30)) hideSwitchLabel.adjustsFontSizeToFitWidth = true hideSwitchLabel.text = "Hide Markers" @@ -23,6 +36,15 @@ let hideMarkerSwitchView = UISwitch(frame: CGRect(x: panelWidth - panelWidth / 2 panel.addSubview(hideSwitchLabel) panel.addSubview(hideMarkerSwitchView) +// Pitch map +let pitchLabel = UILabel(frame: CGRect(x: 0, y: 60, width: 100, height: 30)) +pitchLabel.text = "Pitch" +let pitchSwitch = UISwitch(frame: CGRect(x: panelWidth-panelWidth / 2.0, y: 60, width: 100, height: 50)) +let responder = Responder() +pitchSwitch.addTarget(responder, action: #selector(responder.togglePitch(_:)), forControlEvents: .ValueChanged) +panel.addSubview(pitchLabel) +panel.addSubview(pitchSwitch) + //: # Mapbox Maps /*: @@ -53,6 +75,7 @@ class MapDelegate: NSObject, MGLMapViewDelegate { let av = PlaygroundAnnotationView(reuseIdentifier: "annotation") av.frame = CGRect(x: 0, y: 0, width: 30, height: 30) av.centerOffset = CGVector(dx: -15, dy: -15) + av.flatten = true let centerView = UIView(frame: CGRectInset(av.bounds, 3, 3)) centerView.backgroundColor = UIColor.whiteColor() av.addSubview(centerView) @@ -121,6 +144,7 @@ XCPlaygroundPage.currentPage.liveView = mapView let mapDelegate = MapDelegate() mapView.delegate = mapDelegate +responder.mapView = mapView let tapGesture = UILongPressGestureRecognizer(target: mapDelegate, action: #selector(mapDelegate.handleTap)) mapView.addGestureRecognizer(tapGesture) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 64d8c63d47f..021e0a90fbf 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -619,6 +619,7 @@ - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id annotation; @property (nonatomic, readwrite, nullable) NSString *reuseIdentifier; - +@property (nonatomic, weak) MGLMapView *mapView; @end @implementation MGLAnnotationView @@ -38,6 +41,28 @@ - (void)setCenter:(CGPoint)center center.x += _centerOffset.dx; center.y += _centerOffset.dy; [super setCenter:center]; + + if (_flatten) { + [self updatePitch]; + } +} + +- (void)updatePitch +{ + if (self.mapView.camera.pitch != _oldPitch) + { + CATransform3D t = CATransform3DRotate(CATransform3DIdentity, MGLRadiansFromDegrees(self.mapView.camera.pitch), 1.0, 0, 0); + self.layer.transform = t; + _oldPitch = self.mapView.camera.pitch; + } +} + +- (MGLMapView *)mapView +{ + if (!_mapView) { + _mapView = (MGLMapView *)self.superview.superview; + } + return _mapView; } - (id)actionForLayer:(CALayer *)layer forKey:(NSString *)event diff --git a/platform/ios/src/MGLAnnotationView_Private.h b/platform/ios/src/MGLAnnotationView_Private.h index c9a887b6cc7..cfccb232345 100644 --- a/platform/ios/src/MGLAnnotationView_Private.h +++ b/platform/ios/src/MGLAnnotationView_Private.h @@ -7,6 +7,7 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) id annotation; @property (nonatomic, readwrite, nullable) NSString *reuseIdentifier; +@property (nonatomic, weak) MGLMapView *mapView; @end From d61fc1df784d278b5e5f022bfcdf70aa0b3cbf5f Mon Sep 17 00:00:00 2001 From: Fredrik Karlsson Date: Wed, 18 May 2016 15:39:30 -0700 Subject: [PATCH 2/4] [ios] wip flatten annotation view --- platform/ios/src/MGLAnnotationView.h | 8 ++---- platform/ios/src/MGLAnnotationView.m | 30 +++++++------------- platform/ios/src/MGLAnnotationView_Private.h | 3 +- platform/ios/src/MGLMapView.mm | 8 ++++-- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/platform/ios/src/MGLAnnotationView.h b/platform/ios/src/MGLAnnotationView.h index 98c7c2f6d07..6ac831b8fb6 100644 --- a/platform/ios/src/MGLAnnotationView.h +++ b/platform/ios/src/MGLAnnotationView.h @@ -2,8 +2,6 @@ #import "MGLTypes.h" -@class MGLMapView; - NS_ASSUME_NONNULL_BEGIN /** The MGLAnnotationView class is responsible for representing point-based annotation markers as a view. Annotation views represent an annotation object, which is an object that corresponds to the MGLAnnotation protocol. When an annotation’s coordinate point is visible on the map view, the map view delegate is asked to provide a corresponding annotation view. If an annotation view is created with a reuse identifier, the map view may recycle the view when it goes offscreen. */ @@ -38,11 +36,9 @@ NS_ASSUME_NONNULL_BEGIN /** - Annotation view is not flattened by default. - - Setting this property to true will force the annotation view to tilt according to the associated map view. + Setting this property to YES will force the annotation view to tilt according to the associated map view. */ -@property (nonatomic, assign) BOOL flatten; +@property (nonatomic, assign, getter=isFlattened) BOOL flatten; /** diff --git a/platform/ios/src/MGLAnnotationView.m b/platform/ios/src/MGLAnnotationView.m index d5bbfdc5823..dc087670092 100644 --- a/platform/ios/src/MGLAnnotationView.m +++ b/platform/ios/src/MGLAnnotationView.m @@ -2,9 +2,7 @@ #import "MGLAnnotationView_Private.h" #import "MGLMapView.h" -@interface MGLAnnotationView () { - double _oldPitch; -} +@interface MGLAnnotationView () @property (nonatomic) id annotation; @property (nonatomic, readwrite, nullable) NSString *reuseIdentifier; @@ -37,32 +35,26 @@ - (void)setCenterOffset:(CGVector)centerOffset } - (void)setCenter:(CGPoint)center +{ + [self setCenter:center pitch:0]; +} + +- (void)setCenter:(CGPoint)center pitch:(CGFloat)pitch { center.x += _centerOffset.dx; center.y += _centerOffset.dy; + [super setCenter:center]; if (_flatten) { - [self updatePitch]; + [self updatePitch:pitch]; } } -- (void)updatePitch +- (void)updatePitch:(CGFloat)pitch { - if (self.mapView.camera.pitch != _oldPitch) - { - CATransform3D t = CATransform3DRotate(CATransform3DIdentity, MGLRadiansFromDegrees(self.mapView.camera.pitch), 1.0, 0, 0); - self.layer.transform = t; - _oldPitch = self.mapView.camera.pitch; - } -} - -- (MGLMapView *)mapView -{ - if (!_mapView) { - _mapView = (MGLMapView *)self.superview.superview; - } - return _mapView; + CATransform3D t = CATransform3DRotate(CATransform3DIdentity, MGLRadiansFromDegrees(pitch), 1.0, 0, 0); + self.layer.transform = t; } - (id)actionForLayer:(CALayer *)layer forKey:(NSString *)event diff --git a/platform/ios/src/MGLAnnotationView_Private.h b/platform/ios/src/MGLAnnotationView_Private.h index cfccb232345..c5a65487a21 100644 --- a/platform/ios/src/MGLAnnotationView_Private.h +++ b/platform/ios/src/MGLAnnotationView_Private.h @@ -7,7 +7,8 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic) id annotation; @property (nonatomic, readwrite, nullable) NSString *reuseIdentifier; -@property (nonatomic, weak) MGLMapView *mapView; + +- (void)setCenter:(CGPoint)center pitch:(CGFloat)pitch; @end diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 7afe72bf63f..5bd29836493 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -4381,7 +4381,10 @@ - (void)updateAnnotationViews { [self.glView addSubview:annotationView]; } - annotationView.center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self]; + + CGPoint center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self]; + [annotationView setCenter:center pitch:self.camera.pitch]; + annotationContext.annotationView = annotationView; } } @@ -4393,7 +4396,8 @@ - (void)updateAnnotationViews } else { - annotationView.center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self];; + CGPoint center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self]; + [annotationView setCenter:center pitch:self.camera.pitch]; } } } From 4e35c6e9984fccc141f248d35f1398da22fa2237 Mon Sep 17 00:00:00 2001 From: Fredrik Karlsson Date: Wed, 18 May 2016 16:14:36 -0700 Subject: [PATCH 3/4] [ios] naming conventions --- platform/ios/Mapbox.playground/Contents.swift | 2 +- platform/ios/app/MBXViewController.m | 2 +- platform/ios/src/MGLAnnotationView.h | 2 +- platform/ios/src/MGLAnnotationView.m | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/platform/ios/Mapbox.playground/Contents.swift b/platform/ios/Mapbox.playground/Contents.swift index 5b96ef47d4f..ed48116da9d 100644 --- a/platform/ios/Mapbox.playground/Contents.swift +++ b/platform/ios/Mapbox.playground/Contents.swift @@ -75,7 +75,7 @@ class MapDelegate: NSObject, MGLMapViewDelegate { let av = PlaygroundAnnotationView(reuseIdentifier: "annotation") av.frame = CGRect(x: 0, y: 0, width: 30, height: 30) av.centerOffset = CGVector(dx: -15, dy: -15) - av.flatten = true + av.flat = true let centerView = UIView(frame: CGRectInset(av.bounds, 3, 3)) centerView.backgroundColor = UIColor.whiteColor() av.addSubview(centerView) diff --git a/platform/ios/app/MBXViewController.m b/platform/ios/app/MBXViewController.m index 021e0a90fbf..02090eba3e8 100644 --- a/platform/ios/app/MBXViewController.m +++ b/platform/ios/app/MBXViewController.m @@ -619,7 +619,7 @@ - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id Date: Wed, 18 May 2016 16:33:48 -0700 Subject: [PATCH 4/4] [ios] removed unused property --- platform/ios/src/MGLAnnotationView.m | 1 - 1 file changed, 1 deletion(-) diff --git a/platform/ios/src/MGLAnnotationView.m b/platform/ios/src/MGLAnnotationView.m index aea18015a34..5fcfe2651d0 100644 --- a/platform/ios/src/MGLAnnotationView.m +++ b/platform/ios/src/MGLAnnotationView.m @@ -6,7 +6,6 @@ @interface MGLAnnotationView () @property (nonatomic) id annotation; @property (nonatomic, readwrite, nullable) NSString *reuseIdentifier; -@property (nonatomic, weak) MGLMapView *mapView; @end @implementation MGLAnnotationView