【发布时间】:2020-06-26 05:55:05
【问题描述】:
当我在UIAccessibilityCustomAction 的selector 参数中传递静态/类方法时,它不会被触发。
同样的方法适用于手势识别器/添加目标函数
自定义操作已正确设置和宣布。所以设置没有问题。但是当我双击staticTest 时不会触发。
如果我将实例方法传递给它,它就可以工作。
代码设置不起作用:
// does not work
newView.accessibilityCustomActions?.append(
UIAccessibilityCustomAction(
name: "staticTest test action",
target: ViewController.self,
selector: #selector(ViewController.staticTest)))
代码示例:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let newView = UIView(frame: CGRect(x: 20, y: 20, width: 300, height: 300))
newView.backgroundColor = UIColor.red
view.isUserInteractionEnabled = true
view.addSubview(newView)
newView.isAccessibilityElement = true
// works
newView.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(instanceTest)))
// works
newView.accessibilityCustomActions = [
UIAccessibilityCustomAction(name: "instanceTest test action", target: self, selector: #selector(instanceTest))
]
// works
newView.addGestureRecognizer(
UITapGestureRecognizer(
target: ViewController.self,
action: #selector(ViewController.staticTest)))
// does not work
newView.accessibilityCustomActions?.append(
UIAccessibilityCustomAction(
name: "staticTest test action",
target: ViewController.self,
selector: #selector(ViewController.staticTest)))
}
@objc static func staticTest() -> Bool {
print("staticTest")
return true
}
@objc func instanceTest() -> Bool {
print("InstanceTest")
return true
}
}
【问题讨论】:
标签: ios swift uiaccessibility