ios - 自定义 UILongPressGestureRecognizer 的问题不适用于 VoiceOver

标签 ios swift uigesturerecognizer voiceover uiaccessibility

我在运行 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/

相关文章:

iphone - 在 iOS 中使用图像蒙版动画填充圆形进度表

ios - 尝试访问自定义 UiView 中的高度属性,遇到 "height is a get-only property"错误

ios - 我无法施放 initialViewController

ios - UITableViewCell 错误 : this class is not key value coding-compliant

ios - 来自 Web 请求 JSON 的 Swift 字符串有换行符 - 如何删除换行符?

ios - 在 Swift 4 中使 UISearch 栏透明

javascript - 在 iOS UIWebView 上运行 javascript 函数

ios - 检测手指向上/向下 UITapGestureRecognizer

ios - UIBarButtonItem 长按/短按

ios - UIGestures 与手动控制