ios:在点击 collectionview 单元格或 tableview 单元格时禁用第二个触摸事件

标签 ios uitableview uicollectionview

我有一个带有 CollectionView 的 ViewController,它为 ViewController 中提出的问题加载了四个可能的答案。 当用户选择一个项目时,collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 被调用,其中问题随着答案数组(总是 4)而变化,然后调用 collectionView.reloadData 来重绘新问题和可能的答案。

除了用户连续快速点击某个项目 2 次外,一切都运行良好。 在这种情况下,第一个选择记录答案,然后 collectionview 再次点击(就好像用户在下一个问题上再次点击),从而回答另一个问题。

如果可能的话,我想做的是: 1. 在第一次触摸时禁用触摸事件(重新加载新问题时) 2. collectionView 的 reloadData 完成加载后,重新启用触摸事件。这是我使用取自该线程的自定义 Collection View 类解决的另一个问题 How to tell when UITableView has completed ReloadData?

到目前为止,这是我尝试过的:

func loadNewQuestion {
    //UIApplication.shared.beginIgnoringInteractionEvents()
    //self.view.isUserInteractionEnabled = false
    //change the question, answer, and array of possible answers
    answers = answers.shuffled() //simply shuffle the answers
    //pick a random answer
    let number = Int.random(in: 0 ... 3)
    answer = answers[number] //shuffle again and take first value
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if stillAnswering {
        print("still answering, so skipping touch events")
        return
    }
    stillAnswering = true
    print("not answering")
    let a = answers[indexPath.row]
    if a.descript.lowercased() == questionAnswer.descript.lowercased() //questionAnswer is the correct answer to the question {
        self.loadNewQuestion()
        self.collectionView.reloadData(onComplete: {
            //UIApplication.shared.endIgnoringInteractionEvents()
            //self.view.isUserInteractionEnabled = true
            stillAnswering = false
            })
    } else {
        //warn about wrong answer
        stillAnswering = false
    }
}

我标记了 objective-c 和 swift,因为我不介意用于解决方案的语言,而且我相信 uitableview 和 uicollectionview 的解决方案/问题是相似的。

有什么提示吗?

最佳答案

您可以使用 flag 并在点击正确的答案单元格时将其设置为 true,然后在 reloadData() 的 onComplete block 中将其设置为 false。

例如:

var answerChosen: Bool = false

...

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if answerChosen { return }

    let a = answers[indexPath.row]
    if a.descript.lowercased() == questionAnswer.descript.lowercased() //questionAnswer is the correct answer to the question {
        answerChosen = true
        self.loadNewQuestion()
        self.collectionView.reloadData(onComplete: {
            answerChosen = false    
        })
    } else {
        //warn about wrong answer
    }
}

关于ios:在点击 collectionview 单元格或 tableview 单元格时禁用第二个触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55801690/

相关文章:

ios - NSDate 不是 Objective-C 对象?

ios - swift - 如何删除 NSAttributedString 中的空白(空)行?

ios - Swift 4.2 使用 UICollectionViewCell 添加/删除按钮

iphone - UITableView 中单元格高度变化的动画

iphone - 暂时使单元格可选择

ios - 如何确定自定义 UICollectionViewCell 何时在屏幕上显示为 100%

ios - iOS 中 UITableView 的多列

ios - 将当前 UIViewController 内置动画与其他动画同步

ios - 使用自动布局将自定义 UIView 垂直和水平居中

ios - insertRowsAtIndexPaths 中的 NSInternalInconsistencyException