我在“收集查看单元格”上使用“单击”来调用performSegue
内的collectionView didSelectItemAt
。有时特别是当应用程序首次启动时,performSegue
会延迟,不确定原因,用户再次点击&再次。在这种情况下,多次调用performSegue
并多次按下新视图控制器(抽头数)。这种延迟的任何特殊原因?如果没有,那么我将不得不实施以下内容:
var alreadyTapped = false
override func viewDidAppear(_ animated: Bool) {
alreadyTapped = false
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if alreadyTapped { return }
alreadyTapped = true
performSegue(withIdentifier: Constants.Segue.DETAIL_VC, sender: collectionView.cellForItem(at: indexPath))
}
或者,如果有更好的方法来解决这个问题?
答案 0 :(得分:2)
尝试以下代码,
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
DispatchQueue.main.async {
performSegue(withIdentifier: Constants.Segue.DETAIL_VC, sender: collectionView.cellForItem(at: indexPath))
}
}
答案 1 :(得分:0)
您可以做一个布尔标志...但是我更喜欢延迟...
var didSelectTime = Date()
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
//MARK:- Prevent multiple calls. If elapsed time between calls is less than 1 sec, do nothing
if Date().timeIntervalSince(self.didSelectTime) < 1.0 {
self.didSelectTime = Date()
return
}
self.didSelectTime = Date()
.....
}