Search code examples
iosaccessibilityuiaccessibility

Adding Accessibility to piano style UI element


I'm trying to make a musical keyboard UI element accessible. Just like how GarageBand does it. In other words, at first touch user is told by VoiceOver that they are touching a musical keyboard, and from that point every tap on musical keyboard view plays notes and there’s no further VoiceOver interruptions until user touches outside of the musical keyboard frame.

I have a UICollectionView where each cell represents a musical key and when user taps on it notes are played as expected. However, I have trouble getting this to work like the GB does. For the UICollectionView object, I’ve set accessibilityLabel and have set accessibilityTraits to UIAccessibilityTraitAllowsDirectInteraction. But that doesn’t seem to work. It doesn’t play any notes when VoiceOver is on. On the first tap VoiceOver anounces whatever the accessibilityLabel is set to and then just beeps on every tap.

I have custom UIGestureRecogniser subclass that I use for the collection view cell tap detection. Do I need to do something special under these circumstances?

Any ideas? Do I need to be doing anything else?


Solution

  • Figured it out. Not sure what the reasoning is however.

    Embeded the musical keyboard UICollectionView in another view and made that view accessible with the UIAccessibilityTraitAllowsDirectInteractiontrait. Now it works as expected.