uicollectionView 单元格没有动画(swift4)
uicollectionView Cell not animating (swift4)
我下面的代码有效,因为它在日志文件中打印了一些内容。但是当按下每个单独的单元格时,单元格没有任何反应,期望打印日志文件中的语句。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 2, initialSpringVelocity: 2, animations: {
cell.frame = self.theIssues.bounds
self.theIssues.isScrollEnabled = false
cell.superview?.bringSubview(toFront: cell)
print("d")
}, completion: nil)
}
替换
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
与 cellForItem
let cell = collectionView.cellForItemAtIndexPath(indexPath)
dequeueReusableCell
应该用于为数据源中的项目创建单元格,而不是用于获取可见单元格。要获得可见单元格,请使用 cellForItem
(它由 if let
初始化,因为它可以 return 一个 nil 值 - 这在您的情况下不应该发生,但让我们使用最佳实践):
if let cell = collectionView.cellForItem(at: indexPath) as? whyCollectionViewCell {
UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 2, initialSpringVelocity: 2, animations: {
cell.frame = self.theIssues.bounds
self.theIssues.isScrollEnabled = false
cell.superview?.bringSubview(toFront: cell)
print("d")
}, completion: nil)
}
但是我强烈建议您不要乱动单元格的框架(cell.superview?.bringSubview(toFront: cell)
也非常可疑)- 这是 tableView
的责任。特别是因为您将框架设置为边界 - 这会弄乱单元格的原点,而不仅仅是它的边界。
如果你想做一些效果,我宁愿为单元格的内容(你负责布局)制作动画,或者我什至会考虑拍摄单元格的快照并制作动画。当然,所有这些都取决于您要实现的目标。
我下面的代码有效,因为它在日志文件中打印了一些内容。但是当按下每个单独的单元格时,单元格没有任何反应,期望打印日志文件中的语句。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 2, initialSpringVelocity: 2, animations: {
cell.frame = self.theIssues.bounds
self.theIssues.isScrollEnabled = false
cell.superview?.bringSubview(toFront: cell)
print("d")
}, completion: nil)
}
替换
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! whyCollectionViewCell
与 cellForItem
let cell = collectionView.cellForItemAtIndexPath(indexPath)
dequeueReusableCell
应该用于为数据源中的项目创建单元格,而不是用于获取可见单元格。要获得可见单元格,请使用 cellForItem
(它由 if let
初始化,因为它可以 return 一个 nil 值 - 这在您的情况下不应该发生,但让我们使用最佳实践):
if let cell = collectionView.cellForItem(at: indexPath) as? whyCollectionViewCell {
UIView.animate(withDuration: 1, delay: 0.1, usingSpringWithDamping: 2, initialSpringVelocity: 2, animations: {
cell.frame = self.theIssues.bounds
self.theIssues.isScrollEnabled = false
cell.superview?.bringSubview(toFront: cell)
print("d")
}, completion: nil)
}
但是我强烈建议您不要乱动单元格的框架(cell.superview?.bringSubview(toFront: cell)
也非常可疑)- 这是 tableView
的责任。特别是因为您将框架设置为边界 - 这会弄乱单元格的原点,而不仅仅是它的边界。
如果你想做一些效果,我宁愿为单元格的内容(你负责布局)制作动画,或者我什至会考虑拍摄单元格的快照并制作动画。当然,所有这些都取决于您要实现的目标。