【问题标题】:Trouble animating the selected collection view cell when tapped点击时无法为选定的集合视图单元格设置动画
【发布时间】:2019-12-02 02:32:21
【问题描述】:

这是一个简单的问题,我似乎无法解决问题。

我在集合视图中有一个单元格列表,当我点击它时,我只想让单元格突出显示一下,然后消失,只是一个点击动画。

我尝试在 didSelectItemAt 中更改背景,但它似乎突出显示了错误的单元格。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
        UIView.animate(withDuration: 0.5, animations: {
            cell?.backgroundColor = .lightGray
        }, completion: {
            (value: Bool) in
            cell?.backgroundColor = .white
        })
}

还尝试了我从堆栈中找到的多项内容,但均未成功。

【问题讨论】:

标签: ios swift uicollectionview uicollectionviewcell


【解决方案1】:

BackgroundColor 不能为任何单元格设置动画。一种解决方案是设置 backgroundView 颜色并为其 alpha 设置动画。您还可以插入自己的视图作为背景,并以相反的方向为其 alpha 设置动画以创建更改。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt 
indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
    UIView.animate(withDuration: 0.5, animations: {
        cell?.backgroundColor = UIColor.lightGray.cgColor
    }, completion: {
        (value: Bool) in
        cell?.layer.backgroundColor = UIColor.white.cgColor
    })
}

【讨论】:

  • 动画不是真正的问题。我不知道谁会赞成这个答案
  • 你的问题听起来像动画问题。也许换个问题?
【解决方案2】:
  • 您可以按照建议在alpha 属性的帮助下证明这一点

    UIView.animate(withDuration: 0.5, animations: {
        cell.backgroundColor = .darkGray
        cell.alpha = 0.7
     }, completion: {
        (value: Bool) in
        cell.alpha = 1
     })
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多