diff options
author | Jesse Crocker <Jesse@datamongers.net> | 2016-12-01 11:50:20 -0800 |
---|---|---|
committer | Minh Nguyễn <mxn@1ec5.org> | 2016-12-01 11:50:20 -0800 |
commit | 5dd71d3c3048c1a521f65f3a2f7b13c9c0fed279 (patch) | |
tree | 23243e1cac7a20a70754e02335e8ae16d85dd22a /platform/ios/src | |
parent | b2c2d4ebc4041d4ca5d71bf589dfa5826ec5bf46 (diff) | |
download | qtlocation-mapboxgl-5dd71d3c3048c1a521f65f3a2f7b13c9c0fed279.tar.gz |
[ios, macos] Make tap gesture recognizer fail if it doesn’t do anything (#7246)
* Make tap gesture recognizer fail if it doesn’t do anything.
* fix typo
* Use the right nullable
* update changelog
* Make single click gesture recognizer fail if it doesn’t select an annotation
* fix changelog typo
* Remove singleTapGestureRecognizer from public header
Diffstat (limited to 'platform/ios/src')
-rw-r--r-- | platform/ios/src/MGLMapView.mm | 64 |
1 files changed, 47 insertions, 17 deletions
diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 68a1196fa4..5c365ad87b 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -236,9 +236,8 @@ public: @property (nonatomic, readwrite) UIButton *attributionButton; @property (nonatomic) NS_MUTABLE_ARRAY_OF(NSLayoutConstraint *) *attributionButtonConstraints; @property (nonatomic) UIActionSheet *attributionSheet; - @property (nonatomic, readwrite) MGLStyle *style; - +@property (nonatomic) UITapGestureRecognizer *singleTapGestureRecognizer; @property (nonatomic) UIPanGestureRecognizer *pan; @property (nonatomic) UIPinchGestureRecognizer *pinch; @property (nonatomic) UIRotationGestureRecognizer *rotate; @@ -502,9 +501,10 @@ public: doubleTap.numberOfTapsRequired = 2; [self addGestureRecognizer:doubleTap]; - UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)]; - [singleTap requireGestureRecognizerToFail:doubleTap]; - [self addGestureRecognizer:singleTap]; + _singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)]; + [_singleTapGestureRecognizer requireGestureRecognizerToFail:doubleTap]; + _singleTapGestureRecognizer.delegate = self; + [self addGestureRecognizer:_singleTapGestureRecognizer]; UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTapGesture:)]; twoFingerTap.numberOfTouchesRequired = 2; @@ -1398,6 +1398,27 @@ public: return; } + id<MGLAnnotation>annotation = [self annotationForGestureRecognizer:singleTap persistingResults:YES]; + if(annotation) + { + [self selectAnnotation:annotation animated:YES]; + } + else + { + [self deselectAnnotation:self.selectedAnnotation animated:YES]; + } +} + +/** + Returns the annotation that would be selected by a tap gesture recognizer. + + This is used when a gesture is recognized, and to check if the gesture should be recognized. + + @param singleTap An in progress tap gesture recognizer. + @param persist True to remember the cycleable set of annotations. @see annotationTagAtPoint:persistingResults + */ +- (nullable id <MGLAnnotation>)annotationForGestureRecognizer:(UITapGestureRecognizer*)singleTap persistingResults:(BOOL)persist +{ CGPoint tapPoint = [singleTap locationInView:self]; if (self.userLocationVisible) @@ -1412,19 +1433,19 @@ public: // Get the tap point within the custom hit test layer. tapPointForUserLocation = [singleTap locationInView:self.userLocationAnnotationView]; } - + CALayer *hitLayer = [self.userLocationAnnotationView.hitTestLayer hitTest:tapPointForUserLocation]; - + if (hitLayer) { if ( ! _userLocationAnnotationIsSelected) { - [self selectAnnotation:self.userLocation animated:YES]; + return self.userLocation; } - return; + return nil; } } - + // Handle the case of an offset annotation view by converting the tap point to be the geo location // of the annotation itself that the view represents for (MGLAnnotationView *view in self.annotationContainerView.annotationViews) @@ -1436,21 +1457,19 @@ public: } } } - - MGLAnnotationTag hitAnnotationTag = [self annotationTagAtPoint:tapPoint persistingResults:YES]; + + MGLAnnotationTag hitAnnotationTag = [self annotationTagAtPoint:tapPoint persistingResults:persist]; if (hitAnnotationTag != MGLAnnotationTagNotFound) { if (hitAnnotationTag != _selectedAnnotationTag) { id <MGLAnnotation> annotation = [self annotationWithTag:hitAnnotationTag]; NSAssert(annotation, @"Cannot select nonexistent annotation with tag %u", hitAnnotationTag); - [self selectAnnotation:annotation animated:YES]; + return annotation; } } - else - { - [self deselectAnnotation:self.selectedAnnotation animated:YES]; - } + + return nil; } - (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap @@ -1608,6 +1627,17 @@ public: } } } + else if (gestureRecognizer == _singleTapGestureRecognizer) + { + //Gesture will be recognized if it could deselect an annotation + if(!self.selectedAnnotation) + { + id<MGLAnnotation>annotation = [self annotationForGestureRecognizer:(UITapGestureRecognizer*)gestureRecognizer persistingResults:NO]; + if(!annotation) { + return NO; + } + } + } return YES; } |