我在运行 VoiceOver 时遇到了让自定义长按处理程序工作的问题。
在下面的自定义 UITableViewCell 代码中,我添加了一个带有 UITableViewCell 的长按手势识别器,并在长按时调用 handleVoiceOverLongPress()
。
// called when this custom UITableViewCell class is initialized
func initializeVoiceoverCellView() {
NotificationCenter.default.addObserver(self,
selector: #selector(voiceOverStatusChanged),
name: UIAccessibility.voiceOverStatusDidChangeNotification,
object: nil)
if UIAccessibility.isVoiceOverRunning {
addLongPressGestureRecognizer(withTarget: self, action: #selector(handleVoiceOverLongPress), duration: 1.0)
}
}
@objc func voiceOverStatusChanged() {
if UIAccessibility.isVoiceOverRunning {
addLongPressGestureRecognizer(withTarget: self, action: #selector(handleVoiceOverLongPress), duration: 1.0)
} else {
removeLongPressGestureRecognizer()
}
}
使用上面的代码,handleVoiceOverLongPress()
在 VoiceOver 打开时长按时永远不会被调用。我尝试了一次长按,然后是
最佳答案
I'm having issues getting a custom long press handler to work when VoiceOver is running.
尝试使用下面的代码在您的表格 View 单元格上实现长按手势:
//Gesture definition in your cell implementation for instance.
var yourLongPressGesture: UILongPressGestureRecognizer
yourLongPressGesture = UILongPressGestureRecognizer(target: self,
action: #selector(handleVoiceOverLongPress))
yourLongPressGesture.minimumPressDuration = 0.5
yourLongPressGesture.delaysTouchesBegan = true
yourLongPressGesture.delegate = yourCell
yourCell.addGestureRecognizer(yourLongPressGesture)
//Define what the long press triggers in the following function.
@objc func handleVoiceOverLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state == UIGestureRecognizer.State.ended {
print("LONG PRESS ENDED")
}
else {
print("LONG PRESS IN PROGRESS...")
}
}
不要忘记 long press in the VoiceOver world与此功能未运行时的效果不同:双击并按住压力可获得相同的结果,这一点非常重要。
遵循上面代码片段的基本原理应该可以帮助您使 UILongPressGestureRecognizer 与 VoiceOver 一起工作。
关于ios - 自定义 UILongPressGestureRecognizer 的问题不适用于 VoiceOver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57962945/