简体   繁体   English

点击时无法为选定的集合视图单元格设置动画

[英]Trouble animating the selected collection view cell when tapped

This is a simple question and I can't seem to solve the problem.这是一个简单的问题,我似乎无法解决问题。

I have a list of cells in a collection view and when I tap it I just want the cell to highlight for a bit then disappear just an animation for tapping.我在集合视图中有一个单元格列表,当我点击它时,我只想让单元格突出显示一点,然后消失只是一个点击动画。

I've tried changing the background in my didSelectItemAt but it seemed to be highlighting the wrong cell.我已经尝试在我的 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
        })
}

Also tried multiple things I found from stack and it wasn't working.还尝试了我从堆栈中找到的多种东西,但它不起作用。

BackgroundColor is not animatable for any cell. BackgroundColor 不能为任何单元格设置动画。 One solution is to set backgroundView color and animate its alpha.一种解决方案是设置 backgroundView 颜色并为其 alpha 设置动画。 You could also insert your own views as backgrounds and animate their alphas in opposite directions to create the change.您还可以插入您自己的视图作为背景,并在相反方向上为它们的 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
    })
}
  • You can show that with the help of alpha property as suggested您可以按照建议在alpha属性的帮助下显示

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 轻按UITextField时的动画视图 - Animating view when UITextField tapped 在swift 4.0中点击收集视图单元格时更改标签的文本 - Changing the text of a label when a collection view cell is tapped in swift 4.0 当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器? - How to move another view controller when i tapped any collection view cell (without taping last cell)? 双击集合视图单元格时如何使图像可见? - How do I make image visible when collection view cell is double tapped? 当我们在Ios中点击它时,如何使用图像进行集合视图单元格“放大”和“缩小” - How to do collection view cell “zoom in” and “zoom out” with image when we tapped on it in Ios Obj-C - 点击另一个单元格时取消选择选定的单元格? - Obj-C - Deselect the selected cell when another cell is tapped? Swift - 选择 TableView 单元格时的动画 (didSelectRowAtIndexPath) - Swift - Animating When TableView Cell Selected (didSelectRowAtIndexPath) 允许一次点击和更新集合视图单元格中的一个按钮 - Allow one button in collection view cell to be tapped and updated at a time 选中后如何更改集合视图单元格的背景颜色? - how to change collection view cell background colour when it is selected? 如何找出在集合视图单元格中挖掘的集合视图单元格 - How to find out which collection view cell is tapped inside collection view cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM