ios - 无法更改取消选择动画的动画持续时间
问题描述
我尝试为 UICollectionView 单元格中的单元格实现自定义动画,但我无法实现取消选择动作的动画。
我为 iOS >= 10 使用 swift 和开发项目
这是示例项目 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
}
如何平滑地取消选择?
解决方案
您只需要在代码中实现以下方法,
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)
}
}
推荐阅读
- hadoop - 如何配置 Tensorflow Serving 以服务来自 HDFS 的模型?
- reactjs - React - redux 数组映射
- python - 在 Google Cloud Platform 中使用 jupyter notebook 的 Python 模块和包
- hadoop - 使用 hadoop-functions.sh 启动 hadoop 失败
- xml - 将 XSD 转换为 TypeScript
- r - 更改 ggplot 中的布局
- azure - 如何在 AzureData Factory 中存储元数据输出?
- php - 如何从下面的数组中获取 orderId?
- c# - C# API GetResponse 问题
- tfs - nuget 还原步骤上的路径无效