这是一个简单的问题,我似乎无法解决问题。
我在集合视图中有一个单元格列表,当我点击它时,我只希望该单元格突出显示一点,然后消失只是一个动画以供点击。
我尝试过在didSelectItemAt中更改背景,但是它似乎突出显示了错误的单元格。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 0.5, animations: {
cell?.backgroundColor = .lightGray
}, completion: {
(value: Bool) in
cell?.backgroundColor = .white
})
}
还尝试了我从堆栈中发现的很多东西,但它不起作用。
BackgroundColor不能对任何单元格设置动画。一种解决方案是设置backgroundView颜色并为其alpha设置动画。您也可以将自己的视图作为背景插入,并以相反的方向为其Alpha设置动画以创建更改。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt
indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
UIView.animate(withDuration: 0.5, animations: {
cell?.backgroundColor = UIColor.lightGray.cgColor
}, completion: {
(value: Bool) in
cell?.layer.backgroundColor = UIColor.white.cgColor
})
}
您可以根据建议使用alpha
属性来显示这一点>
UIView.animate(withDuration: 0.5, animations: { cell?.backgroundColor = .darkGray cell.alpha = 0.7 }, completion: { (value: Bool) in cell.alpha = 1 })
}