Is it possible to assign an accessibility action to a UILabel?

前端 未结 5 1579
轻奢々
轻奢々 2021-01-18 19:32

In our current UI, next to certain labels, we have a help-tip button that when clicked, explains the details of what the label references. As such, VoiceOver identifies the

相关标签:
5条回答
  • 2021-01-18 20:01

    In your UILabel subclass, override accessibilityActivate() and implement whatever double-tapping should do:

    override func accessibilityActivate() -> Bool {
        // do things...
        return true
    }
    

    If the action can fail, return false in those instances.

    0 讨论(0)
  • 2021-01-18 20:01

    Group your label and your hint button as one unique accessible element.

    Once done, you can use :

    • The accessibilityActivationPoint property to define the hint button to be triggered when the double tap occurs.
    • The accessibilityActivate method to indicate the action to be done when you double tap your new element.

    According to your environment, I don't recommend to implement a custom action for such a simple use case... the two solutions above should do the job.

    0 讨论(0)
  • 2021-01-18 20:03

    Ok, this was easier than I thought. To make a UILabel respond to accessibility actions similar to how a button does, you simply implement a UITapGestureRecognizer. The Accessibility framework uses that just like any other UIView.

        let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(labelTapped))
        testLabel.userInteractionEnabled = true
        testLabel.addGestureRecognizer(tapGestureRecognizer)
    

    Once you do that, your label will respond to accessibility actions.

    0 讨论(0)
  • 2021-01-18 20:04

    Have your tried adding a UITapGestureRecognizer to the Labels?

    Something like :

    let tapGesture: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapResponse:")
        tapGesture.numberOfTapsRequired = 1
        sampleLabel.userInteractionEnabled =  true
        sampleLabel.addGestureRecognizer(tapGesture)
    
    func tapResponse(recognizer: UITapGestureRecognizer) {
        print("tap")
    }
    
    0 讨论(0)
  • 2021-01-18 20:07

    Absolutely! You can do this by using UIAccessibilityCustomActions on the accessibility element rather than using tap gesture recognizers. This is because accessibility operates differently than normal users and single tapping while the voice over focus lands somewhere will not give you the desired result as in the case of a normal use, nor will it permit you to execute multiple options on the same accessibility element.

    At their recent WWDC, Apple put out an excellent video explaining how to add UIAccessibilityCustomActions to any kind of accessibility element. If you start this video 33 minutes in, you will be able to see how this is implemented.

    Once in place, your Voice Over users will be able to scroll through the options and select the one that most suits his/her intentions, thereby permitting multiple actions to be accessible from the same UILabel.

    0 讨论(0)
提交回复
热议问题