我們可以簡單把Accessibility理解成無障礙化使用疚漆,這個“無障礙”面向群體是視障人士
在iOS中,Accessibility主要分為三部分
- VoiceOver刁赦,旁白讀屏
- Zoom娶聘,縮放
- White on Black,反轉(zhuǎn)顏色
這里主要介紹VoiceOver
什么是VoiceOver
VoiceOver是蘋果手機(jī)上一個給視力不好或者盲人使用應(yīng)用程序的語音輔助軟件 甚脉,即使你看不見也不成問題丸升。輕觸屏幕即可聽到你手指劃過的內(nèi)容,然后運(yùn)用手勢來控制設(shè)備牺氨。VoiceOver 支持 iPhone狡耻、iPad 或 iPod touch 所配備的 app。
如何打開VoiceOver
-
手機(jī)設(shè)備
設(shè)置(Setting)→輔助功能(Accessibility)→旁白(VoiceOver)
- 輕點一下選擇控件
- 輕點兩下激活所選控件
- 三指輕掃滾動視圖
模擬器
打開Accessibility Inspector進(jìn)行調(diào)試
Xcode→Open Developer Tool→Accessibility Inspector
如何支持VoiceOver
VoiceOver并不是萬能的猴凹,并不能兼容開發(fā)者自定義的控件和視圖夷狰,因此作為開發(fā)者,需要通過一些額外的工作讓 APP 可以支持無障礙使用郊霎。
所有的標(biāo)準(zhǔn) UIKit 控件和視圖默認(rèn)支持 VoiceOver沼头,當(dāng)然你也可以通過UIAccessibility 編程接口關(guān)閉或者修改。
如果App使用了用戶自定義的一些控件,這時候就需要用戶通過 UIAccessibility編程接口來支持無障礙
- 什么是AccessibilityElement
VoiceOver是通過訪問元素達(dá)到無障礙使用的进倍,能被VoiceOver訪問的元素叫做AccessibilityElement
土至,UIKit中的基本控件都支持VoiceOver
UILabel,UIButton這些控件的isAccessibilityElement屬性默認(rèn)就是true猾昆,UIView這個屬性默認(rèn)是false - AccessibilityElement的應(yīng)用
一般的AccessibilityElement系統(tǒng)會自動讀出它的內(nèi)容陶因,所以很多情況下不需要做特殊處理應(yīng)用也會支持VoiceOver,但如果需要讀出其他自定義內(nèi)容就需要了解一下幾個屬性- accessibilityLabel:用于描述內(nèi)容“是什么”
- accessibilityHint:用于描述行動“做什么”
- accessibilityValue:用于描述“值是多少”
- accessibilityTraits:一個或多個獨立特征的組合
讀的順序accessibilityLabel
→accessibilityValue
→accessibilityTraits
→accessibilityHint
let count = 2
addButton.accessibilityLabel = "Add count"
addButton.accessibilityHint = "tap to add count"
addButton.accessibilityValue = "\(count)"
addButton.accessibilityTraits = [.button]
// 如果是單一的控件垂蜗,可以不用設(shè)置accessibilityTraits楷扬,系統(tǒng)會自動識別
這段代碼讀的順序:Add count(accessibilityLabel) 2(accessibilityValue) button(accessibilityTraits) tap to add count(accessibilityHint)
自定義視圖如何支持VoiceOver
在前面我們有提到View的isAccessibilityElement
屬性默認(rèn)為false,所以第一步我們需要開啟isAccessibilityElement么抗,使得VoiceOver可以訪問這個ViewUIAccessibilityCustomAction
一個 UIAccessibilityCustomAction 對象代表一個在無障礙對象上執(zhí)行的自定
義操作
let save = UIAccessibilityCustomAction(name: "save the custom view",
target: self,
selector: #selector(tappedSave))
accessibilityCustomActions = [save]
- UIAccessibilityPostNotification
Accessibility提供了一些列的通知來完成特殊的需求
public static func post(notification: UIAccessibility.Notification, argument: Any?)
例如在App中存在錯誤有彈窗彈出覆蓋屏幕的主要部分毅否,在彈窗彈出后,通過調(diào)用這個方法蝇刀,將焦點聚焦到彈窗標(biāo)題上
UIAccessibility.post(notification: .screenChanged, argument: nil)
實際應(yīng)用
假設(shè)現(xiàn)在有一個UITableview與一個自定義Cell螟加,Cell中有一個按鈕,現(xiàn)在需求是可以訪問Cell與Cell中的按鈕吞琐,且分別讀出不同的內(nèi)容捆探,那么我們要怎么實現(xiàn)這個需求呢
我們首先回想一下前面提到的,當(dāng)一個View(或子類)激活isAccessibilityElement時站粟,它的子視圖都變?yōu)椴豢稍L問了黍图,所以單純設(shè)置Cell為訪問元素然后再訪問子元素這個思路是行不通的
- 方案一
我們可以這么做,Cell不激活為isAccessibilityElement奴烙,但是蘋果內(nèi)部是可以將Cell作為整一個訪問元素且設(shè)置它的accessibilityLabel助被,完成button基本設(shè)置后,再設(shè)置button的accessibilityHint就可以實現(xiàn)我們想要的效果
private func setupAccessibility() {
accessibilityLabel = "cell"
nameLabel.accessibilityLabel = nameLabel.text
nameLabel.isAccessibilityElement = true
logoView.isAccessibilityElement = true
logoView.accessibilityLabel = "logo"
button.accessibilityHint = "save the info"
}
- 方案二
我們可以激活Cell為isAccessibilityElement切诀,然后對其添加accessibilityCustomActions
綁定對應(yīng)的方法揩环,這個方案,nameLabel
幅虑、logoView
丰滑、button
雖然已經(jīng)激活為isAccessibilityElement,但依然是無法訪問的倒庵,所以你是看不到訪問的框去到button或者其他控件那里的
private func setupAccessibility() {
isAccessibilityElement = true
accessibilityLabel = "cell"
nameLabel.accessibilityLabel = nameLabel.text
nameLabel.isAccessibilityElement = true
logoView.isAccessibilityElement = true
logoView.accessibilityLabel = "logo"
button.isAccessibilityElement = true
button.accessibilityHint = "save the info"
let action = UIAccessibilityCustomAction(name: "action", target: self, selector: #selector(didTapButton))
accessibilityCustomActions = [action]
}
Tips
- 經(jīng)試驗褒墨,accessibilityHint只有在真機(jī)的情況下才會讀出來,而且與上一個屬性之間間隔大概有1秒的時間擎宝,debug的時候要留意
- 當(dāng)這整個View作為VoiceOver的訪問元素郁妈,View中的subview都會變?yōu)椴豢稍L問,如果需要都支持VoiceOver绍申,需要改變他們的層級關(guān)系圃庭,且都開啟isAccessibilityElement
- UIAccessibilityCustomAction觸發(fā)條件在Voice Over的情況下,選中到該元素后,通過上劃或者下劃來選擇需要觸發(fā)的事件
參考文檔
Accessibility Programming Guide for iOS
iOS Accessibility Tutorial: Getting Started
Making Apps More Accessible With Custom Actions
如果大家有其他解決方案歡迎留言交流
支持原創(chuàng)剧腻,版權(quán)所有
未經(jīng)授權(quán)請勿轉(zhuǎn)載