diff --git a/platform/ios/src/MGLAnnotationContainerView.m b/platform/ios/src/MGLAnnotationContainerView.m index cd9c990ef..9a823c839 100644 --- a/platform/ios/src/MGLAnnotationContainerView.m +++ b/platform/ios/src/MGLAnnotationContainerView.m @@ -35,4 +35,18 @@ } } +#pragma mark UIAccessibility methods + +- (UIAccessibilityTraits)accessibilityTraits { + return UIAccessibilityTraitAdjustable; +} + +- (void)accessibilityIncrement { + [self.superview.superview accessibilityIncrement]; +} + +- (void)accessibilityDecrement { + [self.superview.superview accessibilityDecrement]; +} + @end diff --git a/platform/ios/src/MGLAnnotationView.mm b/platform/ios/src/MGLAnnotationView.mm index 6f1d94c3e..31657dbf4 100644 --- a/platform/ios/src/MGLAnnotationView.mm +++ b/platform/ios/src/MGLAnnotationView.mm @@ -1,6 +1,8 @@ #import "MGLAnnotationView.h" #import "MGLAnnotationView_Private.h" -#import "MGLMapView.h" +#import "MGLMapView_Internal.h" + +#import "NSBundle+MGLAdditions.h" #include @@ -103,4 +105,43 @@ return [super actionForLayer:layer forKey:event]; } +#pragma mark UIAccessibility methods + +- (BOOL)isAccessibilityElement { + return !self.hidden; +} + +- (UIAccessibilityTraits)accessibilityTraits { + return UIAccessibilityTraitButton | UIAccessibilityTraitAdjustable; +} + +- (NSString *)accessibilityLabel { + return [self.annotation respondsToSelector:@selector(title)] ? self.annotation.title : super.accessibilityLabel; +} + +- (NSString *)accessibilityValue { + return [self.annotation respondsToSelector:@selector(subtitle)] ? self.annotation.subtitle : super.accessibilityValue; +} + +- (NSString *)accessibilityHint { + return NSLocalizedStringWithDefaultValue(@"ANNOTATION_A11Y_HINT", nil, nil, @"Shows more info", @"Accessibility hint"); +} + +- (CGRect)accessibilityFrame { + CGRect accessibilityFrame = self.frame; + CGRect minimumFrame = CGRectInset({ self.center, CGSizeZero }, + -MGLAnnotationAccessibilityElementMinimumSize.width / 2, + -MGLAnnotationAccessibilityElementMinimumSize.height / 2); + accessibilityFrame = CGRectUnion(accessibilityFrame, minimumFrame); + return accessibilityFrame; +} + +- (void)accessibilityIncrement { + [self.superview accessibilityIncrement]; +} + +- (void)accessibilityDecrement { + [self.superview accessibilityDecrement]; +} + @end \ No newline at end of file diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index ee4346f27..1ad758dc7 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -98,7 +98,6 @@ const CGFloat MGLAnnotationImagePaddingForHitTest = 5; /// Distance from the callout’s anchor point to the annotation it points to. const CGFloat MGLAnnotationImagePaddingForCallout = 1; -/// Minimum size of an annotation’s accessibility element. const CGSize MGLAnnotationAccessibilityElementMinimumSize = CGSizeMake(10, 10); /// Unique identifier representing a single annotation in mbgl. @@ -2021,6 +2020,13 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) MGLAnnotationContext &annotationContext = _annotationContextsByAnnotationTag.at(annotationTag); id annotation = annotationContext.annotation; + // Let the annotation view serve as its own accessibility element. + MGLAnnotationView *annotationView = annotationContext.annotationView; + if (annotationView && annotationView.superview) + { + return annotationView; + } + // Lazily create an accessibility element for the found annotation. if ( ! annotationContext.accessibilityElement) { @@ -2028,17 +2034,8 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) } // Update the accessibility element. - MGLAnnotationView *annotationView = annotationContext.annotationView; - CGRect annotationFrame; - if (annotationView && annotationView.superview) - { - annotationFrame = [self convertRect:annotationView.bounds fromView:annotationView]; - } - else - { - MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; - annotationFrame = [self frameOfImage:annotationImage.image centeredAtCoordinate:annotation.coordinate]; - } + MGLAnnotationImage *annotationImage = [self imageOfAnnotationWithTag:annotationTag]; + CGRect annotationFrame = [self frameOfImage:annotationImage.image centeredAtCoordinate:annotation.coordinate]; CGPoint annotationFrameCenter = CGPointMake(CGRectGetMidX(annotationFrame), CGRectGetMidY(annotationFrame)); CGRect minimumFrame = CGRectInset({ annotationFrameCenter, CGSizeZero }, -MGLAnnotationAccessibilityElementMinimumSize.width / 2, @@ -2075,17 +2072,28 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) { return 1; } - if ( ! [element isKindOfClass:[MGLAnnotationAccessibilityElement class]] && - element != self.attributionButton) + + std::vector visibleAnnotations = [self annotationTagsInRect:self.bounds]; + + MGLAnnotationTag tag = MGLAnnotationTagNotFound; + if ([element isKindOfClass:[MGLAnnotationView class]]) + { + id annotation = [(MGLAnnotationView *)element annotation]; + tag = [self annotationTagForAnnotation:annotation]; + } + else if ([element isKindOfClass:[MGLAnnotationAccessibilityElement class]]) + { + tag = [(MGLAnnotationAccessibilityElement *)element tag]; + } + else if (element == self.attributionButton) + { + return !!self.userLocationAnnotationView + visibleAnnotations.size(); + } + else { return NSNotFound; } - std::vector visibleAnnotations = [self annotationTagsInRect:self.bounds]; - if (element == self.attributionButton) - { - return !!self.userLocationAnnotationView + visibleAnnotations.size(); - } std::sort(visibleAnnotations.begin(), visibleAnnotations.end()); auto foundElement = std::find(visibleAnnotations.begin(), visibleAnnotations.end(), ((MGLAnnotationAccessibilityElement *)element).tag); diff --git a/platform/ios/src/MGLMapView_Internal.h b/platform/ios/src/MGLMapView_Internal.h index 0538dd039..6225e1174 100644 --- a/platform/ios/src/MGLMapView_Internal.h +++ b/platform/ios/src/MGLMapView_Internal.h @@ -1,5 +1,8 @@ #import +/// Minimum size of an annotation’s accessibility element. +extern const CGSize MGLAnnotationAccessibilityElementMinimumSize; + @interface MGLMapView (Internal) /** Triggers another render pass even when it is not necessary. */