Search code examples
iosswiftvoiceovericarouseluiaccessibility

How to have accessible swipe action for iCarousel


I am trying to make an accessible app. The problem arises when I wanted to have standard (Three-finger swipe right or left) behavior with iCarousel. Since iCarousel is not fully accessible by nature and it just lets user to have items be User intractable.

I done some research, there are some workarounds but I also don't want to implement UIAccessibilityCustomAction cause this will not give the user easy to swipe ability.


Solution

  • You can override and implement accessibilityScroll(_:) method as:

    extension iCarousel {
        override open func accessibilityScroll(_ direction: UIAccessibilityScrollDirection) -> Bool {
            super.accessibilityScroll(direction)
    
            if direction == UIAccessibilityScrollDirection.left {
                self.scroll(byOffset: 1, duration: 1.0)
            }
            if direction == UIAccessibilityScrollDirection.right {
                self.scroll(byOffset: -1, duration: 1.0)
            }
            return true
        }
    }
    

    and post a desire (e.g. page number) in iCarousel delegate method carouselDidEndScrollingAnimation, you may also change the accessibilityLabel and other accessibility related values here.

    func carouselDidEndScrollingAnimation(_ carousel: iCarousel) {
        self.carousel.accessibilityLabel = accLabels[currentItemIndex]
        self.carousel.accessibilityValue = accValues[currentItemIndex]
        UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification,
                                        "item \(currentItemIndex + 1) of \(self.items.count)")
    }
    

    Also in viewDidLoad set the initial accessibilityLabel as well as:

    self.carousel.accessibilityTraits = UIAccessibilityTraitUpdatesFrequently
    

    I hope this helps someone.