无法更改动画持续时间以取消选择动画

时间:2019-05-18 19:04:46

标签: ios swift uicollectionview

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

我为iOS> = 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

如何平滑地对取消选择进行动画处理?

2 个答案:

答案 0 :(得分:3)

您只需要在代码中实现以下方法,

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

答案 1 :(得分:0)

该解决方案非常容易编写 收集视图单元中的问题

请用那些替换类中的函数

 public func setup() {

    self.backgroundColor = UIColor.gray
}

public func configureForSelected() {
    self.backgroundColor = UIColor.red
}

public func configureForUnselected() {
    self.backgroundColor = UIColor.gray
}

已设置

  public func setup() {

    self.contentView.backgroundColor = UIColor.gray
}

public func configureForSelected() {
    self.contentView.backgroundColor = UIColor.red
}

public func configureForUnselected() {
    self.contentView.backgroundColor = UIColor.gray
}