LongPres Hold Switch Not Evaluating to Ended/Failed (Drop and Drop Freeze)

LongPressHold Switch Not Evaluating to Ended/Failed (Drop & Drop Freeze)

我有一个 UITableView,我向其添加了 UILongPressGestureRecognizer。当一个单元格被按住时,它会触发一个评估手势状态的开关:

switch state { 
    case UIGestureRecognizerState.began:
        //Takes a snapshot of the cell, shows image and hides cell
        print("User held cell.")
    case UIGestureRecognizerState.changed:
        //Runs when cell-snapshot is dragged 
        print("LocationInView is: \(location)")
    default:
        //Runs when the user "lets go" of the cell
        print("User let go.")
 }

在测试过程中,如果将单元格拖到 table 上方或下方并放开,则会冻结。如果拖回table然后松开,没关系。

table 不会占据整个屏幕,如果用户试图将其拖出 table 的边界,单元格会停在 table 的边缘。当你在 'out of bounds' 时放手,细胞会永久冻结在边缘。

如果开关告诉我手势完成,我可以简单地隐藏单元格,但问题是默认值永远不会触发。如果用户在离开 table 后松开手指,则开关永远不会计算为默认值。

现在 UIGestureRecognizerState 有 "cancelled"、"ended" 和 "failed" 状态,为什么它们不触发默认值?我有显示正常手指抬起的打印语句:

并在 table 区域外抬起手指:

这个问题的答案在代码中并不明显,但在图像中。正在将单元格 "out of bounds" 拖离其边缘 table/to。一旦到达那里,它就无法传递要评估的 indexValue!

这是解决方案,因为大多数 tableView drag/drop 代码将首先检查 nil,包括我自己的代码:

    if indexPath != nil {
        switch state {
        case UIGestureRecognizerState.began:
            ...

棘手的部分是如何处理现在无法评估识别器状态的情况...是吗 end/fail/cancel?如果我要重新开始,我会尝试将识别器添加到全屏视图中,这样无论用户的手指移到哪里,我都可以始终监视 gestureRecognizer 状态。