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

╄→гoц情女王★ 提交于 2019-12-01 18:11:16

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.

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")
}

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.

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

Once done, you can use :

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.

Brian Li

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!