【发布时间】:2019-04-22 22:03:28
【问题描述】:
我有一个带有 CollectionView 的 ViewController,它加载 ViewController 中提出的问题的四个可能答案。 当用户选择一个项目时,会调用 collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) ,其中问题会随着答案数组(始终为 4)而变化,然后调用 collectionView.reloadData 以重绘新问题并可能的答案。
一切正常,除了用户连续快速点击一个项目 2 次。 在这种情况下,第一个选择会记录答案,然后 collectionview 会再次点击(就像用户再次点击下一个问题一样)并因此回答另一个问题。
如果可能的话,我想做的是: 1.在第一次触摸时禁用触摸事件(重新加载新问题时) 2. 一旦collectionView 的reloadData 完成加载,重新启用触摸事件。这是我使用取自该线程How to tell when UITableView has completed ReloadData? 的自定义集合视图类解决的另一个问题
我尝试使用 view.userInteractionEnabled = false/true 和 UIApplication.shared.beginIgnoringInteractionEvents() 和 UIApplication.shared.endIgnoringInteractionEvents() 禁用触摸事件,但没有成功。
这是我迄今为止尝试过的:
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 的解决方案/问题是相似的。
有什么提示吗?
【问题讨论】:
-
loadNewQuestion 只是简单地打乱现有数组并选择另一个随机答案.. 我还是更新了问题
标签: ios uitableview uicollectionview