繁体   English   中英

如何使用Swift淡入淡出TableViewCell的背景色

[英]How to fade in/out the background color of a tableviewcell with Swift

我有一个tableview,当我收到一条消息时,它会重新加载,然后我希望接收到该消息的单元格更改其背景颜色一秒钟,然后再更改回以前的颜色(有或没有淡入/淡出)

我知道哪个单元收到该代码的消息:

    if changeCell == indexPath.row { 

         cell.viewCell.backgroundColor = UIColor.red 

    }

viewCell是我放入单元格以更轻松地更改单元格背景颜色的视图

我进行了大量搜索以找出UIView.animate ,但每种解决方案都始于UIView.animate ....当我将其放置时,它更改了tableview外部视图的颜色

您想要的是UIView动画类方法。

    let oldColor = cell.viewCell.backgroundColor
    UIView.animate(withDuration: 0.5, animations: {
        cell.viewCell.backgroundColor = UIColor.red
        }, completion: { _ in
            UIView.animate(withDuration: 0.5) {
                cell.viewCell.backgroundColor = oldColor
            }
    })

为动画添加以下选项:

  • UIViewAnimationOptions.transitionCrossDissolve
  • UIViewAnimationOptions.allowAnimatedContent

例:

fileprivate var color1 = UIColor.clear
fileprivate var color2 = UIColor.red
fileprivate func animateCell(_ cell: UITableViewCell) {

    UIView.animate(withDuration: 1.0, delay: 0.0, options: [.transitionCrossDissolve, .allowAnimatedContent], animations: {
        cell.contentView.backgroundColor = self.color2
    }) { (_) in
        UIView.animate(withDuration: 1.0, animations: {
            cell.contentView.backgroundColor = self.color1
        })
    }

}

我通过在单元格中添加UIImageView并设置其Alpha来解决此问题。 我无法设置backgroundColor进行动画处理。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM