gpt4 book ai didi

ios - 无法更改取消选择动画的动画持续时间

转载 作者:搜寻专家 更新时间:2023-10-31 22:24:10 35 4
gpt4 key购买 nike

我尝试为 UICollectionView 单元格中的单元格实现自定义动画,但我无法为取消选择操作实现动画。

我为 iOS 使用 swift 和开发项目 >= 10

是示例项目 https://drive.google.com/file/d/1nUVVBFBA7N6ZHNIKOdHf21j4rv1w0SfL/view?usp=sharing

示例项目中的代码

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

var selectedIndex: Int?

if let selectedIndexPathes = collectionView.indexPathsForSelectedItems {
if (selectedIndexPathes.count > 0) {
selectedIndex = selectedIndexPathes[0].item
}
}

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell

if (selectedIndex == indexPath.item) {
cell.configureForSelected()
}
else {
cell.configureForUnselected()
}

return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

var selectedIndex: Int?

if let selectedIndexPathes = collectionView.indexPathsForSelectedItems {
if (selectedIndexPathes.count > 0) {
selectedIndex = selectedIndexPathes[0].item
}
}

if (selectedIndex == indexPath.item) {
return CGSize(width: collectionView.frame.width, height: 200)
}

return CGSize(width: collectionView.frame.width, height: 100)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

UIView.animate(withDuration: 3.0) {
collectionView.performBatchUpdates({

if (self.lastSelectedIndexPath != nil) {
let lastCell = collectionView.cellForItem(at: self.lastSelectedIndexPath!) as! CollectionViewCell
lastCell.configureForUnselected()
}

let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
cell.configureForSelected()
}, completion: nil)
}
lastSelectedIndexPath = indexPath
}

enter image description here

我如何才能顺利地为取消选择设置动画?

最佳答案

你只需要在你的代码中实现下面的方法,

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
UIView.animate(withDuration: 3.0) {
collectionView.performBatchUpdates({

if (self.lastSelectedIndexPath != nil) {
let lastCell = collectionView.cellForItem(at: self.lastSelectedIndexPath!) as! CollectionViewCell
lastCell.configureForUnselected()
}

}, completion: nil)
}
}

enter image description here

关于ios - 无法更改取消选择动画的动画持续时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202048/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com