我觉得这是一个愚蠢的问题,但我找不到一个解决方案。我有一个集合视图,每个单元格都有不同的背景颜色,当用户滚动集合时,我会更改标签textColor以匹配当前视图中单元格的背景颜色。标签在集合视图之外。
检测哪个单元格在视图中并更改颜色的工作,现在我想应用一个过渡效果到标签textColor,以便它在用户滚动时看起来更漂亮。唯一的问题是,我的过渡动画也与集合视图上的滚动动画交互,这看起来非常奇怪。
有没有一种方法可以使我的过渡只应用于标签文本颜色?或者我可能需要改变我检测哪个单元格在视图中以及如何触发动画的方式。
代码:
@IBOutlet weak var balanceLabel: UILabel!
@IBOutlet weak var overviewCollection: UICollectionView!
func configureVisibleIndexPath() {
let visibleCells = overviewCollection.indexPathsForVisibleItems
visibleCells.forEach {
indexPath in
if let cell = overviewCollection.cellForItem(at: indexPath), overviewCollection.bounds.contains(cell.frame) {
print("visible row is \(indexPath.row)")
let visibleIndexPath = indexPath.row
switch visibleIndexPath {
case 0:
UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {
self.balanceLabel.textColor = UIColor(named: "ywGreen")
})
case 1:
UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {
self.balanceLabel.textColor = UIColor(named: "ywYellow")
})
case 2:
UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {
self.balanceLabel.textColor = UIColor(named: "ywBlue")
})
case 3:
UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {
self.balanceLabel.textColor = UIColor(named: "ywWhite")
})
default:
UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {
self.balanceLabel.textColor = UIColor(named: "ywGreen")
})
}
}
}
}
任何帮助感激不尽!
1条答案
按热度按时间mlmc2os51#
将
with:
参数从view
更改为self.balanceLabel
。而不是这个:
执行以下操作: