【问题标题】:Can't change animation duration for deselect anim无法更改取消选择动画的动画持续时间
【发布时间】:2019-05-18 19:04:46
【问题描述】:

我尝试为 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
    }

如何平滑地取消选择?

【问题讨论】:

    标签: ios swift uicollectionview


    【解决方案1】:

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

    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)
        }
    }
    

    【讨论】:

    • 是的,这是工作。 omartarek32 回答也解决了我的问题。哪种解决方案更好?
    • 是的,这也更好,但是如果您要在表格视图中获取子视图,那么上面的@omartarek32 解决方案将不起作用,您需要使用我的解决方案对表格视图的子视图执行操作细胞。
    • 通过调用您的函数,您只是在为表格视图单元格内的容器视图设置动画,而不是表格视图单元格本身。因此,表格视图高度保持不变。因此,如果您想反映高度,那么您需要像我一样手动取消选择单元格,或者您可以直接为表格视图单元格设置动画,如上面@omartarek32 的答案中所述。
    【解决方案2】:

    从您的代码中可以轻松解决问题 集合视图单元格中的问题

    请用这些替换这个类中的函数

     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
    }
    

    【讨论】:

    • 是的,这是工作。你能解释一下吗?为什么动画依赖于这种变化?
    • 那是因为 contentView 本身没有动画,它自己的单元格动画很好,但它的内容视图没有动画很好。请尝试在“CollectionViewCell”的“awakeFromNib()”函数中添加以下行,并检查行为:self.contentView.backgroundColor = .green
    猜你喜欢
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多