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

[ios] fixes #5036 draggable annotation views #5373

Merged
merged 1 commit into from
Jun 24, 2016
Merged
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
28 changes: 28 additions & 0 deletions platform/ios/app/MBXAnnotationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,32 @@ - (void)setSelected:(BOOL)selected animated:(BOOL)animated
self.layer.borderWidth = selected ? 2.0 : 0;
}

- (void)setDragState:(MGLAnnotationViewDragState)dragState animated:(BOOL)animated
{
[super setDragState:dragState animated:NO];

switch (dragState) {
case MGLAnnotationViewDragStateNone:
break;
case MGLAnnotationViewDragStateStarting: {
[UIView animateWithDuration:.4 delay:0 usingSpringWithDamping:.4 initialSpringVelocity:.5 options:UIViewAnimationOptionCurveLinear animations:^{
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2, 2);
} completion:nil];
break;
}
case MGLAnnotationViewDragStateDragging:
break;
case MGLAnnotationViewDragStateCanceling:
break;
case MGLAnnotationViewDragStateEnding: {
[UIView animateWithDuration:.4 delay:0 usingSpringWithDamping:.4 initialSpringVelocity:.5 options:UIViewAnimationOptionCurveLinear animations:^{
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
} completion:nil];
break;
}
}

}


@end
14 changes: 14 additions & 0 deletions platform/ios/app/MBXViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ - (IBAction)handleLongPress:(UILongPressGestureRecognizer *)longPress
{
if (longPress.state == UIGestureRecognizerStateBegan)
{
/*
CGPoint point = [longPress locationInView:longPress.view];
NSArray *features = [self.mapView visibleFeaturesAtPoint:point];
NSString *title;
Expand All @@ -427,6 +428,7 @@ - (IBAction)handleLongPress:(UILongPressGestureRecognizer *)longPress
pin.subtitle = [[[MGLCoordinateFormatter alloc] init] stringFromCoordinate:pin.coordinate];
// Calling `addAnnotation:` on mapView is not required since `selectAnnotation:animated` has the side effect of adding the annotation if required
[self.mapView selectAnnotation:pin animated:YES];
*/
}
}

Expand Down Expand Up @@ -590,6 +592,12 @@ - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAn
// uncomment to flatten the annotation view against the map when the map is tilted
// this currently causes severe performance issues when more than 2k annotations are visible
// annotationView.flat = YES;

// uncomment to make the annotation view draggable
// also note that having two long press gesture recognizers on overlapping views (`self.view` & `annotationView`) will cause weird behaviour
// comment out the pin dropping functionality in the handleLongPress: method in this class to make draggable annotation views play nice
annotationView.draggable = YES;


// uncomment to force annotation view to maintain a constant size when the map is tilted
// by default, annotation views will shrink and grow as the move towards and away from the
Expand All @@ -603,6 +611,12 @@ - (MGLAnnotationView *)mapView:(MGLMapView *)mapView viewForAnnotation:(id<MGLAn
return annotationView;
}

- (void)mapView:(MGLMapView *)mapView didDragAnnotationView:(nonnull MGLAnnotationView *)annotationView toCoordinate:(CLLocationCoordinate2D)coordinate
{
MGLPointAnnotation *annotation = (MGLPointAnnotation *)annotationView.annotation;
annotation.coordinate = coordinate;
}

- (BOOL)mapView:(__unused MGLMapView *)mapView annotationCanShowCallout:(__unused id <MGLAnnotation>)annotation
{
return YES;
Expand Down
27 changes: 27 additions & 0 deletions platform/ios/src/MGLAnnotationView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ NS_ASSUME_NONNULL_BEGIN

@protocol MGLAnnotation;

typedef NS_ENUM(NSUInteger, MGLAnnotationViewDragState) {
MGLAnnotationViewDragStateNone = 0, // View is sitting on the map.
MGLAnnotationViewDragStateStarting, // View is beginning to drag.
MGLAnnotationViewDragStateDragging, // View is being dragged.
MGLAnnotationViewDragStateCanceling, // View dragging was cancelled and will be returned to its starting positon.
MGLAnnotationViewDragStateEnding // View was dragged.
};

/** 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. */
@interface MGLAnnotationView : UIView

Expand Down Expand Up @@ -69,6 +77,25 @@ NS_ASSUME_NONNULL_BEGIN
*/
@property (nonatomic, assign, getter=isEnabled) BOOL enabled;

/**
Setting this property to YES will make the view draggable. Long-press followed by a pan gesture will start to move the
view around the map. `-mapView:didDragAnnotationView:toCoordinate:` will be called when a view is dropped.
*/
@property (nonatomic, assign, getter=isDraggable) BOOL draggable;

/**
All states are handled automatically when `draggable` is set to YES.
Custom animations can be achieved by overriding setDragState:animated:
*/
@property (nonatomic, readonly) MGLAnnotationViewDragState dragState;

/**
Called when the `dragState` changes.

Implementer may override this method in order to customize animations in subclasses.
*/
- (void)setDragState:(MGLAnnotationViewDragState)dragState animated:(BOOL)animated NS_REQUIRES_SUPER;

/**
Setting this property to YES will cause the annotation view to shrink as it approaches the horizon and grow as it moves away from the
horizon when the associated map view is tilted. Conversely, setting this property to NO will ensure that the annotation view maintains
Expand Down
136 changes: 134 additions & 2 deletions platform/ios/src/MGLAnnotationView.mm
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#import "MGLAnnotationView.h"
#import "MGLAnnotationView_Private.h"
#import "MGLAnnotation.h"
#import "MGLMapView_Internal.h"

#import "NSBundle+MGLAdditions.h"

#include <mbgl/util/constants.hpp>

@interface MGLAnnotationView ()
@interface MGLAnnotationView () <UIGestureRecognizerDelegate>

@property (nonatomic, readwrite, nullable) NSString *reuseIdentifier;
@property (nonatomic, readwrite, nullable) id <MGLAnnotation> annotation;
@property (nonatomic, weak) UIPanGestureRecognizer *panGestureRecognizer;
@property (nonatomic, weak) UILongPressGestureRecognizer *longPressRecognizer;
@property (nonatomic, weak) MGLMapView *mapView;

@end

Expand Down Expand Up @@ -62,6 +66,11 @@ - (void)setCenter:(CGPoint)center pitch:(CGFloat)pitch

[super setCenter:center];

// Omit applying a new transformation while the view is being dragged.
if (self.dragState == MGLAnnotationViewDragStateDragging) {
return;
}

if (self.flat)
{
[self updatePitch:pitch];
Expand Down Expand Up @@ -108,6 +117,129 @@ - (void)updateScaleForPitch:(CGFloat)pitch
}
}

#pragma mark - Draggable

- (void)setDraggable:(BOOL)draggable
{
[self willChangeValueForKey:@"draggable"];
_draggable = draggable;
[self didChangeValueForKey:@"draggable"];

if (draggable)
{
[self enableDrag];
}
else
{
[self disableDrag];
}
}

- (void)enableDrag
{
if (!_longPressRecognizer)
{
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
_longPressRecognizer = recognizer;
}

if (!_panGestureRecognizer)
{
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
_panGestureRecognizer = recognizer;
}
}

- (void)disableDrag
{
[self removeGestureRecognizer:_longPressRecognizer];
[self removeGestureRecognizer:_panGestureRecognizer];
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
{
switch (sender.state) {
case UIGestureRecognizerStateBegan:
self.dragState = MGLAnnotationViewDragStateStarting;
break;
case UIGestureRecognizerStateChanged:
self.dragState = MGLAnnotationViewDragStateDragging;
break;
case UIGestureRecognizerStateCancelled:
self.dragState = MGLAnnotationViewDragStateCanceling;
break;
case UIGestureRecognizerStateEnded:
self.dragState = MGLAnnotationViewDragStateEnding;
break;
case UIGestureRecognizerStateFailed:
self.dragState = MGLAnnotationViewDragStateNone;
break;
case UIGestureRecognizerStatePossible:
break;
}
}

- (void)handlePan:(UIPanGestureRecognizer *)sender
{
CGPoint center = [sender locationInView:sender.view.superview];
[self setCenter:center pitch:self.mapView.camera.pitch];

if (sender.state == UIGestureRecognizerStateEnded) {
self.dragState = MGLAnnotationViewDragStateNone;
}
}

- (void)setDragState:(MGLAnnotationViewDragState)dragState
{
[self setDragState:dragState animated:YES];
}

- (void)setDragState:(MGLAnnotationViewDragState)dragState animated:(BOOL)animated
{
[self willChangeValueForKey:@"dragState"];
_dragState = dragState;
[self didChangeValueForKey:@"dragState"];

if (dragState == MGLAnnotationViewDragStateStarting)
{
[self.superview bringSubviewToFront:self];
}

if (dragState == MGLAnnotationViewDragStateEnding)
{
if ([self.mapView.delegate respondsToSelector:@selector(mapView:didDragAnnotationView:toCoordinate:)])
{
CGPoint offsetAdjustedCenter = self.center;
offsetAdjustedCenter.x -= self.centerOffset.dx;
offsetAdjustedCenter.y -= self.centerOffset.dy;

CLLocationCoordinate2D coordinate = [self.mapView convertPoint:offsetAdjustedCenter toCoordinateFromView:self.mapView];
[self.mapView.delegate mapView:self.mapView didDragAnnotationView:self toCoordinate:coordinate];
}
}
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
BOOL isDragging = self.dragState == MGLAnnotationViewDragStateDragging;

if ([gestureRecognizer isKindOfClass:UIPanGestureRecognizer.class] && !(isDragging))
{
return NO;
}

return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
{
// Allow mbgl to drive animation of this view’s bounds.
Expand Down Expand Up @@ -157,4 +289,4 @@ - (void)accessibilityDecrement {
[self.superview accessibilityDecrement];
}

@end
@end
3 changes: 3 additions & 0 deletions platform/ios/src/MGLAnnotationView_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

NS_ASSUME_NONNULL_BEGIN

@class MGLMapView;

@interface MGLAnnotationView (Private)

@property (nonatomic, readwrite, nullable) NSString *reuseIdentifier;
@property (nonatomic, readwrite, nullable) id <MGLAnnotation> annotation;
@property (nonatomic, weak) MGLMapView *mapView;

- (void)setCenter:(CGPoint)center pitch:(CGFloat)pitch;

Expand Down
2 changes: 1 addition & 1 deletion platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4527,7 +4527,7 @@ - (void)updateAnnotationViews

CGPoint center = [self convertCoordinate:annotationContext.annotation.coordinate toPointToView:self];
[annotationView setCenter:center pitch:self.camera.pitch];

annotationView.mapView = self;
annotationContext.annotationView = annotationView;
}
}
Expand Down
12 changes: 12 additions & 0 deletions platform/ios/src/MGLMapViewDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,18 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (void)mapView:(MGLMapView *)mapView didDeselectAnnotationView:(MGLAnnotationView *)annotationView;

/**
Tells the delegate that one if its annotation views was dragged to a new coordinate.

In order to make the new location persistent, you have to update the `coordinate` property of the associated annotation.

@param mapView The map view containing the annotation view.
@param annotationView The annotation view that was dragged.
@param coordinate The coordinate that the annotation view was dropped on.

*/
- (void)mapView:(MGLMapView *)mapView didDragAnnotationView:(MGLAnnotationView *)annotationView toCoordinate:(CLLocationCoordinate2D)coordinate;

@end

NS_ASSUME_NONNULL_END