From 7dc13194c735102273b961cf59ad43a23cb36491 Mon Sep 17 00:00:00 2001 From: Delisa Mason Date: Fri, 4 Sep 2015 16:20:01 -0700 Subject: [PATCH] Add iOS 7 compatible fallback for accessibilityElements --- SleepModel/HEMBreakdownViewController.h | 5 +++++ SleepModel/HEMBreakdownViewController.m | 19 ++++++++++++++++-- .../HEMSleepSummaryCollectionViewCell.m | 20 ++++++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/SleepModel/HEMBreakdownViewController.h b/SleepModel/HEMBreakdownViewController.h index 4a9ea895..5637d06f 100644 --- a/SleepModel/HEMBreakdownViewController.h +++ b/SleepModel/HEMBreakdownViewController.h @@ -24,4 +24,9 @@ @property (nonatomic, weak) IBOutlet UILabel* itemTitle2; @property (nonatomic, weak) IBOutlet UILabel* itemValue1; @property (nonatomic, weak) IBOutlet UILabel* itemValue2; + +/** + * Convenience container for accessibility elements + */ +@property (nonatomic, strong) NSArray* computedAccessibilityElements; @end \ No newline at end of file diff --git a/SleepModel/HEMBreakdownViewController.m b/SleepModel/HEMBreakdownViewController.m index 8a2902dd..b04b0741 100644 --- a/SleepModel/HEMBreakdownViewController.m +++ b/SleepModel/HEMBreakdownViewController.m @@ -185,7 +185,7 @@ - (UICollectionViewCell *)metricCellInCollectionView:(UICollectionView *)collect [collectionView dequeueReusableCellWithReuseIdentifier:[HEMMainStoryboard breakdownLineCellReuseIdentifier] forIndexPath:indexPath]; cell.isAccessibilityElement = NO; - cell.accessibilityElements = @[]; + cell.computedAccessibilityElements = @[]; [self collectionView:collectionView updateMetricInCell:cell atIndexPath:indexPath inPosition:0]; [self collectionView:collectionView updateMetricInCell:cell atIndexPath:indexPath inPosition:1]; return cell; @@ -210,7 +210,7 @@ - (void)collectionView:(UICollectionView *)collectionView frame.origin.x = position * width; frame.size.width = width; element.accessibilityFrame = frame; - cell.accessibilityElements = [cell.accessibilityElements arrayByAddingObject:element]; + cell.computedAccessibilityElements = [cell.computedAccessibilityElements arrayByAddingObject:element]; } - (NSAttributedString *)titleForItemAtIndexPath:(NSIndexPath *)indexPath position:(NSUInteger)position { @@ -352,4 +352,19 @@ @implementation HEMBreakdownSummaryCell @end @implementation HEMBreakdownLineCell + +- (id)accessibilityElementAtIndex:(NSInteger)index { + if (self.computedAccessibilityElements.count > index) + return self.computedAccessibilityElements[index]; + return nil; +} + +- (NSInteger)accessibilityElementCount { + return self.computedAccessibilityElements.count; +} + +- (NSInteger)indexOfAccessibilityElement:(id)element { + return [self.computedAccessibilityElements indexOfObject:element]; +} + @end diff --git a/SleepModel/HEMSleepSummaryCollectionViewCell.m b/SleepModel/HEMSleepSummaryCollectionViewCell.m index 1643a1c4..3d226aed 100644 --- a/SleepModel/HEMSleepSummaryCollectionViewCell.m +++ b/SleepModel/HEMSleepSummaryCollectionViewCell.m @@ -37,13 +37,31 @@ - (void)awakeFromNib { self.messageContainerView.accessibilityLabel = NSLocalizedString(@"timeline.accessibility-label.breakdown", nil); self.messageContainerView.accessibilityHint = NSLocalizedString(@"timeline.accessibility-hint.breakdown", nil); self.messageContainerView.accessibilityTraits = UIAccessibilityTraitButton; - self.accessibilityElements = @[ self.sleepScoreGraphView, self.messageContainerView ]; } + - (void)prepareForReuse { [super prepareForReuse]; [self.sleepScoreGraphView setLoading:NO]; } +- (NSInteger)accessibilityElementCount { + return 2; +} + +- (id)accessibilityElementAtIndex:(NSInteger)index { + if (index == 0) + return self.sleepScoreGraphView; + return self.messageContainerView; +} + +- (NSInteger)indexOfAccessibilityElement:(id)element { + if ([element isEqual:self.sleepScoreGraphView]) + return 0; + else if ([element isEqual:self.messageContainerView]) + return 1; + return NSNotFound; +} + - (void)setLoading:(BOOL)loading { [self.sleepScoreGraphView setLoading:loading]; }