我有一个UITableViewCell
,其中包含一个具有一些子辅助功能元素的视图。该视图相当长,比单个屏幕更长。
[当用户左右滑动以浏览子元素时,所选元素有时不在屏幕上。当他稍后双击时,将无法触发该元素的动作。
为什么不起作用?如何使其正常工作?
为什么有时所选元素不在屏幕外?如何在屏幕上制作?
这里是sample code。
为什么不起作用?如何使其正常工作?
使用VoiceOver OFF❌, VoiceOver ONtap
函数会在您触摸<< AND >>时将其打印出来,如果它可见,因为轻击手势仅在屏幕上发生。 使用GestureRecognizer
处理手势。一种解决方案可能是使用accessibilityActivate方法,该方法在a11y元素上双击时触发。 我建议创建一个标签类,在其中实现此功能:
class myLabel:UILabel {
init(frame: CGRect, index: Int) {
super.init(frame: frame)
text = "\(index)"
isAccessibilityElement = true
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func accessibilityActivate() -> Bool {
print(text!)
return true
}
@objc func tap(sender: UITapGestureRecognizer) {
guard sender.state == .recognized else { return }
print(sender.view!)
}
}
LongView
类具有以下代码来定义其行为:
class LongView: UIView {
override var isAccessibilityElement: Bool {
get { return false }
set { }
}
override var accessibilityElementsHidden: Bool {
get { return false }
set { }
}
override init(frame: CGRect) {
super.init(frame: frame)
let screenWidth = UIScreen.main.bounds.width
var x: CGFloat = 20
var y: CGFloat = 20
let width: CGFloat = 200
let height: CGFloat = 40
let spacing: CGFloat = 20
for index in 0..<20 {
let label = myLabel(frame: CGRect(x: x, y: y, width: width, height: height),
index: index)
addSubview(label)
x += width + spacing
if x + width > screenWidth {
y += height + spacing
x = spacing
}
}
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override var intrinsicContentSize: CGSize {
CGSize(width: UIScreen.main.bounds.width,
height: subviews.map { $0.frame.maxY }.max()! + 20)
}
}
并且表视图单元格类如下:
class Cell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let longView = LongView()
longView.accessibilityElementsHidden = false
contentView.addSubview(longView)
longView.translatesAutoresizingMaskIntoConstraints = false
longView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
longView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
longView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
longView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
}
现在,您会注意到:
GestureRecognizer
功能。解决UITableViewCell内长视图内UIAccessibilityElement焦点问题的理由