Ios 如何使用Swift淡入/淡出tableviewcell的背景色

Ios 如何使用Swift淡入/淡出tableviewcell的背景色,ios,uitableview,uicolor,Ios,Uitableview,Uicolor,我有一个tableview,当我收到消息时,它会重新加载,然后我希望收到消息的单元格更改其背景颜色一秒钟,然后再更改回以前的颜色(有或没有淡入/淡出) 我知道哪个单元格收到了带有该代码的消息: if changeCell == indexPath.row { cell.viewCell.backgroundColor = UIColor.red } viewCell是我放在单元格中的一个视图,可以更轻松地更改单元格的背景色 我搜索了很多来找到它,但是每

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

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

    if changeCell == indexPath.row { 

         cell.viewCell.backgroundColor = UIColor.red 

    }
viewCell是我放在单元格中的一个视图,可以更轻松地更改单元格的背景色


我搜索了很多来找到它,但是每个解决方案都是从
UIView.animate
开始的。。。。当我放置它时,它改变了tableview外部视图的颜色

UIView animate方法族就是您想要的

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

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

}

为动画添加以下选项:

  • 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来解决这个问题。我无法用背景色来制作动画

我通过在单元格中添加UIImageView并设置其alpha来解决这个问题。我无法用背景色来制作动画

您使用的是xcode 8和swift 3.0吗?@Himanshumuradiya Yes您使用的是xcode 8和swift 3.0吗?@Himanshumuradiya yesHi感谢您的回答,但所有单元格都在淡入/淡出谢谢您的回答,但所有单元格都在淡入/淡出