[英]gradient keeps switching between cells in collection view in swift
I have a gradient background for my collection view cell and each gradient is different depending on what tag the cell is (eg Christmas is a purple gradient, birthday is a green gradient) however when scrolling through the collection view, the gradients keep on changing cells. 我的收藏夹视图单元格具有渐变背景,并且每个渐变都取决于该单元格的标记(例如,圣诞节是紫色渐变,生日是绿色渐变),但是在收藏夹视图中滚动时,渐变会不断变化。 is there a way to fix this from happening?
有办法解决这个问题吗?
this is the code I use to set the gradient to the background view. 这是我用来将渐变设置为背景视图的代码。 it is in
cellForItemAt
它在
cellForItemAt
cell.mainView.setGradientBackground(colours: self.getColourFromTag(tag: self.lists[indexPath.item].tag))
.setGradientBackground
is an extension of UIView which just sets a gradient to the background. .setGradientBackground
是UIView的扩展,它仅将渐变设置为背景。 shown here: 如图所示:
func setGradientBackground(colours: [CGColor]) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = bounds
gradientLayer.colors = colours
gradientLayer.locations = [0.0, 1.0]
gradientLayer.startPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.0)
layer.insertSublayer(gradientLayer, at: 0)
}
I use the method below to get what colours are in the gradient 我使用下面的方法来获取渐变中的颜色
func getColourFromTag(tag: String) -> [CGColor] {
if tag == "Christmas" {
return [Colours.gradients.festive.start.cgColor, Colours.gradients.festive.end.cgColor]
}else if tag == "Birthday" {
return [Colours.gradients.lime.start.cgColor, Colours.gradients.lime.end.cgColor]
}else if tag == "Valentines Day" {
return [Colours.gradients.strawberry.start.cgColor, Colours.gradients.strawberry.end.cgColor]
}else if tag == "Charity" {
return [Colours.gradients.blueberry.start.cgColor, Colours.gradients.blueberry.end.cgColor]
}else if tag == "Event" {
return [Colours.gradients.fire.start.cgColor, Colours.gradients.fire.end.cgColor]
}else{
return [Colours.gradients.midnight.start.cgColor, Colours.gradients.midnight.end.cgColor]
}
}
I have tried appending each [colour] into an array, then placing that into the .setGradientBackground
at the indexPath.item
like so: 我尝试将每种[color]附加到数组中,然后将其放在
.setGradientBackground
的indexPath.item
如下所示:
var colours = [[CGColor]]()
colours[indexPath.item] = self.getColourFromTag(tag: self.lists[indexPath.item].tag)
cell.mainView.setGradientBackground(colours: colours[indexPath.item])
however, this does not work since it is out of range. 但是,这超出了范围,因此不起作用。 Does anyone have a solution?
有没有人有办法解决吗? thank you.
谢谢。
Reading your code in setGradientBackground()
, you always init a new CAGradientLayer
and insert it at the lowest position in your cell. 在
setGradientBackground()
读取代码,您总是会初始化一个新的CAGradientLayer
并将其插入到单元格中的最低位置。 Every time setGradientBackground()
gets called again, the new gradient layer will be positioned below all other gradients you already inserted. 每次再次调用
setGradientBackground()
时,新的渐变层将位于您已插入的所有其他渐变下面。
Try to get a already existing CAGradientLayer
from sublayers and set the new colors. 尝试从子层获取已经存在的
CAGradientLayer
并设置新颜色。
var gradientLayer = CAGradientLayer()
if let sublayers = layer.sublayers {
for sublayer in sublayers {
if let gLayer = sublayer as? CAGradientLayer {
gradientLayer = gLayer
break
}
}
}
gradientLayer.frame = bounds
gradientLayer.colors = colours
gradientLayer.locations = [0.0, 1.0]
gradientLayer.startPoint = CGPoint(x: 1.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.0)
layer.insertSublayer(gradientLayer, at: 0)
EDIT 编辑
This is a more swift like version (Swift 4.2) using compactMap()
for getting the first existing gradient layer in layer.sublayers
: 这是使用像版本更迅速(SWIFT 4.2)
compactMap()
用于获取在所述第一现有的梯度层layer.sublayers
:
if let existingLayer = (layer.sublayers?.compactMap { $0 as? CAGradientLayer })?.first {
gradientLayer = existingLayer
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.