From 39cb9302636ec7367d5f00d213a407eefe720b5c Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Fri, 12 Jul 2019 10:25:42 -0400 Subject: [PATCH] accessibility and page control fixes --- MVMCoreUI/Molecules/Carousel.swift | 36 +++++++++++++------ MVMCoreUI/Molecules/MVMCoreUIPageControl.m | 3 +- .../MoleculeCollectionViewCell.swift | 4 ++- .../Strings/en.lproj/Localizable.strings | 3 ++ .../Strings/es-MX.lproj/Localizable.strings | 3 ++ .../Strings/es.lproj/Localizable.strings | 3 ++ 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/MVMCoreUI/Molecules/Carousel.swift b/MVMCoreUI/Molecules/Carousel.swift index cb5d7243..27cc59f6 100644 --- a/MVMCoreUI/Molecules/Carousel.swift +++ b/MVMCoreUI/Molecules/Carousel.swift @@ -57,6 +57,7 @@ open class Carousel: ViewConstrainingView { collectionView.delegate = self collectionView.showsHorizontalScrollIndicator = false collectionView.backgroundColor = .clear + collectionView.isAccessibilityElement = false addSubview(collectionView) pinView(toSuperView: collectionView) @@ -117,7 +118,7 @@ open class Carousel: ViewConstrainingView { // Sets up the row data with buffer cells on each side (for illusion of endless scroll... also has one more buffer cell on each side in case we can peek that cell). loop = true molecules?.insert(newMolecules.last!, at: 0) - molecules?.insert(newMolecules[(newMolecules.count - 1)], at: 0) + molecules?.insert(newMolecules[(newMolecules.count - 2)], at: 0) molecules?.append(newMolecules.first!) molecules?.append(newMolecules[1]) } @@ -140,6 +141,7 @@ open class Carousel: ViewConstrainingView { var pagingView: (UIView & MVMCoreUIPagingProtocol)? = nil if let json = json { pagingView = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: json, delegateObject: delegateObject, constrainIfNeeded: true) as? (UIView & MVMCoreUIPagingProtocol) + pagingView?.isAccessibilityElement = true } addPaging(view: pagingView, position: (json?.optionalCGFloatForKey("position") ?? 20)) } @@ -196,7 +198,7 @@ open class Carousel: ViewConstrainingView { } let currentPage = pager.currentPage() localSelf.pageIndex = currentPage - self?.collectionView.scrollToItem(at: IndexPath(row: localSelf.currentIndex, section: 0), at: (self?.itemAlignment ?? .left), animated: true) + localSelf.goTo(localSelf.currentIndex, animated: !UIAccessibility.isVoiceOverRunning) }) } self.pagingView = pagingView @@ -244,12 +246,27 @@ extension Carousel: UICollectionViewDataSource { protocolCell.setWithJSON(moleculeInfo.molecule, delegateObject: nil, additionalData: nil) protocolCell.updateView(collectionView.bounds.width) } + + if indexPath.row == currentIndex { + cell.accessibilityElementsHidden = false +// self.accessibilityElements = @[cell.containerView, self.pageControl]; + } else { + cell.accessibilityElementsHidden = true + } return cell } } extension Carousel: UIScrollViewDelegate { + + func goTo(_ index: Int, animated: Bool) { + collectionView.cellForItem(at: IndexPath(row: self.currentIndex, section: 0))?.accessibilityElementsHidden = true + self.currentIndex = index + collectionView.cellForItem(at: IndexPath(row: self.currentIndex, section: 0))?.accessibilityElementsHidden = false + self.collectionView.scrollToItem(at: IndexPath(row: self.currentIndex, section: 0), at: self.itemAlignment, animated: animated) + } + func handleUserOnBufferCell() { guard loop else { return @@ -257,8 +274,7 @@ extension Carousel: UIScrollViewDelegate { let lastPageIndex = numberOfPages + 1 let goToIndex = {(index: Int) in - self.currentIndex = index - self.collectionView.scrollToItem(at: IndexPath(row: self.currentIndex, section: 0), at: self.itemAlignment, animated: false) + self.goTo(index, animated: false) self.collectionView.layoutIfNeeded() self.pagingView?.setPage(self.pageIndex) } @@ -279,11 +295,11 @@ extension Carousel: UIScrollViewDelegate { // Checks if the user is not paging but attempting to drag endlessly and goes out of bounds. Caps the index. if let separatorWidth = (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.minimumLineSpacing { let itemWidth = collectionView.bounds.width * itemWidthPercent - let index = Int(scrollView.contentOffset.x / (itemWidth + separatorWidth)) + let index = scrollView.contentOffset.x / (itemWidth + separatorWidth) let lastCellIndex = collectionView(collectionView, numberOfItemsInSection: 0) - 1 - if index < 0 { + if index < 1 { self.currentIndex = 0 - } else if index > lastCellIndex { + } else if index > CGFloat(lastCellIndex - 1) { self.currentIndex = lastCellIndex } } @@ -326,9 +342,7 @@ extension Carousel: UIScrollViewDelegate { } // Cap the index. - currentIndex = min(max(cellToSwipeTo, 0), lastCellIndex) - - collectionView.scrollToItem(at: IndexPath(row: currentIndex, section: 0), at: itemAlignment, animated: true) + goTo(min(max(cellToSwipeTo, 0), lastCellIndex), animated: true) } // To give the illusion of endless scrolling. Since we are always calling scrollToItem we can assume finished paging in here. @@ -339,5 +353,7 @@ extension Carousel: UIScrollViewDelegate { pagingView?.setPage(pageIndex) showPeaking(true) + + //UIAccessibility.post(notification: .layoutChanged, argument: currentIndex) UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification,currentCell.containerView); } } diff --git a/MVMCoreUI/Molecules/MVMCoreUIPageControl.m b/MVMCoreUI/Molecules/MVMCoreUIPageControl.m index e5855f7d..cbfe0300 100644 --- a/MVMCoreUI/Molecules/MVMCoreUIPageControl.m +++ b/MVMCoreUI/Molecules/MVMCoreUIPageControl.m @@ -302,7 +302,7 @@ static CGFloat const IndicatorRectangleHeight = 4; #pragma mark - MVMCoreUIPagingProtocol -- (void)setPage:(NSUInteger)page { +- (void)setPage:(NSInteger)page { self.currentPage = page; } @@ -349,6 +349,7 @@ static CGFloat const IndicatorRectangleHeight = 4; if ((index < self.numberOfPages && index >= 0) || self.alwaysSendingControlEvent) { [self setCurrentPage:index animated:NO]; [self sendActionsForControlEvents:UIControlEventValueChanged]; + self.pagingTouchBlock(self); } } diff --git a/MVMCoreUI/Molecules/MoleculeCollectionViewCell.swift b/MVMCoreUI/Molecules/MoleculeCollectionViewCell.swift index 23850e70..82d7f4a2 100644 --- a/MVMCoreUI/Molecules/MoleculeCollectionViewCell.swift +++ b/MVMCoreUI/Molecules/MoleculeCollectionViewCell.swift @@ -30,6 +30,9 @@ open class MoleculeCollectionViewCell: UICollectionViewCell, MVMCoreUIMoleculeVi guard peakingCover.superview == nil else { return } + isAccessibilityElement = false + contentView.isAccessibilityElement = false + // Covers the card when peaking. peakingCover.backgroundColor = .white peakingCover.alpha = 0 @@ -109,7 +112,6 @@ open class MoleculeCollectionViewCell: UICollectionViewCell, MVMCoreUIMoleculeVi self.peakingRightArrow.alpha = peaking ? 1 : 0 self.peakingLeftArrow.alpha = peaking ? 1 : 0 self.peakingCover.alpha = peaking ? 0.5 : 0 - print("\(self.peakingCover.alpha)") } if animated { UIView.animate(withDuration: 0.4, animations: animation) diff --git a/MVMCoreUI/SupportingFiles/Strings/en.lproj/Localizable.strings b/MVMCoreUI/SupportingFiles/Strings/en.lproj/Localizable.strings index 96088cab..2e7677f3 100644 --- a/MVMCoreUI/SupportingFiles/Strings/en.lproj/Localizable.strings +++ b/MVMCoreUI/SupportingFiles/Strings/en.lproj/Localizable.strings @@ -40,6 +40,9 @@ "AccOn" = "on"; "AccOff" = "off"; "AccToggleHint" = "double tap to toggle"; +// Carousel +"MVMCoreUIPageControl_currentpage_index" = "page %ld of %ld"; +"MVMCoreUIPageControlslides_currentpage_index" = "slide %ld of %ld"; //Styler "CountDownDay" = " day"; "CountDownHour" = " hour"; diff --git a/MVMCoreUI/SupportingFiles/Strings/es-MX.lproj/Localizable.strings b/MVMCoreUI/SupportingFiles/Strings/es-MX.lproj/Localizable.strings index a006b218..bf4f4e47 100644 --- a/MVMCoreUI/SupportingFiles/Strings/es-MX.lproj/Localizable.strings +++ b/MVMCoreUI/SupportingFiles/Strings/es-MX.lproj/Localizable.strings @@ -39,6 +39,9 @@ "AccOn" = "encendido"; "AccOff" = "apagado"; "AccToggleHint" = "toca dos veces para alternar"; +// Carousel +"MVMCoreUIPageControl_currentpage_index" = "página %ld de %ld"; +"MVMCoreUIPageControlslides_currentpage_index" = "diapositiva %ld of %ld"; //Styler "CountDownDay" = " día"; "CountDownHour" = " hora"; diff --git a/MVMCoreUI/SupportingFiles/Strings/es.lproj/Localizable.strings b/MVMCoreUI/SupportingFiles/Strings/es.lproj/Localizable.strings index a006b218..bf4f4e47 100644 --- a/MVMCoreUI/SupportingFiles/Strings/es.lproj/Localizable.strings +++ b/MVMCoreUI/SupportingFiles/Strings/es.lproj/Localizable.strings @@ -39,6 +39,9 @@ "AccOn" = "encendido"; "AccOff" = "apagado"; "AccToggleHint" = "toca dos veces para alternar"; +// Carousel +"MVMCoreUIPageControl_currentpage_index" = "página %ld de %ld"; +"MVMCoreUIPageControlslides_currentpage_index" = "diapositiva %ld of %ld"; //Styler "CountDownDay" = " día"; "CountDownHour" = " hora";