目前,我有一个collectionView,它唯一的功能是让用户选择一种颜色。当用户选择一种颜色(单击一个单元格)时,一个选定的指示符 ( UIView) 在 内部进行动画处理cell,让用户知道该特定颜色已被选中。核心功能在那里,但我的问题更多地基于 UI。当我滚动时,单元格认为我正在单击它,它会设置isSelected为真一秒钟,直到我将手指从单元格中移开。这会导致动画在不需要的时间发生。所以,我认为问题出shouldSelectItemAt在CollectionViewDelegate方法上。用UIGestureRecognizer我可以衡量是否sender state有.began或.ended。我的问题会要求这个或类似的东西吗?
以下是选择collectionView委托方法。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = colorPreviewCollectionView.cellForItem(at: indexPath) as! TrayColorPreviewCell
// stuff with cell happens here
}
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
let cell = colorPreviewCollectionView.cellForItem(at: indexPath) as! TrayColorPreviewCell
guard cell.isSelected else { return true }
return false
}
在TrayColorPreviewCell(custom UICollectionViewCell) 类中,我使用isSelected来运行动画。此动画是滚动时重复发生的动画。
override var isSelected: Bool {
didSet {
changeSelectionStatus()
}
}
func changeSelectionStatus() {
if isSelected {
// animation to indicate selection
} else {
// animation to indicate changed selection
}
}