自定义 Rotor 的 API?
解读
“Rotor” 是 iOS 辅助功能里 VoiceOver 的高级导航方式:用户双指旋转即可在“标题”“链接”“表格”等系统预设节点间切换。
国内大厂(阿里、美团、京东、B 站等)在信息密度极高的业务页面(搜索筛选项、直播间弹幕、商品 SKU 选择器)普遍需要“自定义语义节点”,让视障用户一次旋转即可跳到“优惠”“评价”“同款”等业务区块,而不用逐行扫读。
面试官问“自定义 Rotor 的 API”,不是考你“知道有这功能”,而是考:
- 是否真正在 Xcode 里写过 AccessibilityCustomRotor、并理解其生命周期;
- 能否把“业务语义”抽象成“搜索关键字”并保证性能;
- 是否能在 SwiftUI 与 UIKit 混合工程里统一实现;
- 是否知道国内审核与隐私合规对“辅助功能数据”无特殊收集限制,但需避免泄露用户个人标签。
一句话:用最小代码量,让 VoiceOver 用户“两指一转”就能精准跳到你定义的业务节点,且对主线程零卡顿。
知识点
- UIAccessibilityCustomRotor / UIAccessibilityCustomRotorItemResult
• 仅 iOS 10+ 可用,系统每转一格就回调 rotor(_:resultFor:) -> UIAccessibilityCustomRotorItemResult?
• 返回的 itemResult 必须指向一个遵守 UIAccessibilityIdentification 的对象(UIView 或 UIAccessibilityElement)。 - 搜索谓词 predicate
• 系统把用户“旋转方向”封装成 UIAccessibilityCustomRotorSearchPredicate,包含 currentItem、searchDirection、queryString。
• 你需要在业务模型层维护“有序关键字数组”,用二分或哈希快速定位下一项,保证 <1 ms。 - 容器同步
• 若页面是 UITableView/UICollectionView,必须在 rotor 跳转前预加载 cell,否则 itemResult 指向不可见元素会触发系统“无法到达”提示。
• 在 cell 复用池里,记得在 prepareForReuse 里把 customRotor 置空,防止“僵尸关键字”。 - SwiftUI 桥接
• 用 UIViewRepresentable 包装 UIHostingController,在 makeUIView 里给 rootView 挂 accessibilityCustomRotors;
• iOS 16+ 可直接在 View 上 .accessibilityRotor(<name>) { predicate in … },但内部仍走 UIKit 桥接,性能一样。 - 调试与灰度
• Xcode Accessibility Inspector 的“Rotor” 面板只能看到系统级,看不到自定义;必须用真机 VoiceOver 验证。
• 国内灰度常通过“辅助功能开关”埋点,监听 UIAccessibilityVoiceOverStatusDidChangeNotification,不上传用户标识,合规。 - 常见坑
• 把 rotor 关键字写死为中文,未做国际化,导致海外用户看到乱码;
• 在 rotor 回调里同步访问 CoreData/网络,主线程卡 16 ms 以上直接被系统 kill 掉;
• 忘记在 viewDidDisappear 里把 customRotors 置空,造成“野指针”崩溃,CrashReporter 里显示 _accessibilityCustomRotorCallback。
答案
下面给出“商品详情页”实战代码,覆盖 UIKit + SwiftUI 混合场景,可直接搬进面试白板,10 行核心,其余解释一句带过。
// 1. 定义业务关键字
private let bizKeywords = ["优惠", "评价", "详情", "推荐", "同款"]
// 2. 在容器 VC 里挂 rotor
final class DetailViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 预加载数据,保证 rotor 回调不碰 IO
self.setupRotors()
}
private func setupRotors() {
let rotor = UIAccessibilityCustomRotor(name: "商品区块") { predicate -> UIAccessibilityCustomRotorItemResult? in
// 3. 快速定位
guard let current = predicate.currentItem?.targetElement else { return nil }
let currentIndex = bizKeywords.firstIndex {
current.accessibilityLabel?.contains($0) ?? false
} ?? -1
let nextIndex: Int
switch predicate.searchDirection {
case .next:
nextIndex = (currentIndex + 1) % bizKeywords.count
case .previous:
nextIndex = (currentIndex - 1 + bizKeywords.count) % bizKeywords.count
@unknown default: return nil
}
let keyword = bizKeywords[nextIndex]
// 4. 在视图树找目标
guard let target = self.view.accessibilityElement(labelContains: keyword) else { return nil }
return UIAccessibilityCustomRotorItemResult(target: target, targetRange: nil)
}
// 5. 挂到容器
self.accessibilityCustomRotors = [rotor]
}
}
// 6. 给 UIView 写个扩展,面试时可口述“用 DFS 找 label”
extension UIView {
func accessibilityElement(labelContains text: String) -> UIView? {
if self.accessibilityLabel?.contains(text) == true { return self }
for sub in subviews {
if let found = sub.accessibilityElement(labelContains: text) { return found }
}
return nil
}
}
SwiftUI 侧一句话带过:
.accessibilityRotor("商品区块") { predicate in
// 同上逻辑,返回 AccessibilityRotorEntry
}
主线程零 IO、关键字数组常驻内存、搜索复杂度 O(1),VoiceOver 用户两指旋转即可循环跳转“优惠→评价→详情→推荐→同款”,国内审核无隐私收集风险。
拓展思考
- 直播高并发弹幕
弹幕每秒 200 条,如何让用户“旋转到主播弹幕”?
• 预过滤“主播 UID”索引,维护循环链表, rotor 回调只返回链表 next 指针,复杂度 O(1);
• 用 CADisplayLink 限制刷新,避免 rotor 触发时正在批量插入 cell。 - 多语言卖场
中文“优惠”对应英文“Coupon”,如何一套代码支持?
• 在 rotor 初始化时用 NSLocalizedString 动态生成关键字数组,保证 VoiceOver 语言切换时实时刷新;
• 注意阿拉伯语 RTL,searchDirection 需要镜像。 - 与 Apple Watch 联动
watchOS 10 也支持 AccessibilityCustomRotor,但屏幕更小,通常只给 3 个关键字。
• 用 WatchConnectivity 把 iPhone 当前 rotor 索引同步到 Watch,保证双端导航一致;
• 审核时需声明“辅助功能数据仅在设备本地使用”,否则苹果会打回。 - 性能极限验证
在 iPhone 6s 这种国内仍有 3% 用户的机型上,用 Instruments 的 Accessibility template 跑 1000 次 rotor 旋转,CPU 占用不得超过 5%,否则面试可被判“工程化经验不足”。