渐变保持在集合视图中的单元格之间快速切换 [英] gradient keeps switching between cells in collection view in swift

查看:19
本文介绍了渐变保持在集合视图中的单元格之间快速切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的收藏视图单元格有一个渐变背景,每个渐变都不同,具体取决于单元格的标签(例如圣诞节是紫色渐变,生日是绿色渐变)但是当滚动浏览收藏视图时,渐变会保持关于改变细胞.有没有办法解决这个问题?

I have a gradient background for my collection view cell and each gradient is different depending on what tag the cell is (e.g 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?

这是我用来将渐变设置为背景视图的代码.它在 cellForItemAt

this is the code I use to set the gradient to the background view. it is in cellForItemAt

cell.mainView.setGradientBackground(colours: self.getColourFromTag(tag: self.lists[indexPath.item].tag))

.setGradientBackground 是 UIView 的扩展,它只是将渐变设置为背景.此处显示:

.setGradientBackground is an extension of UIView which just sets a gradient to the background. 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]
    }
}

我尝试将每个 [color] 附加到一个数组中,然后将其放入 indexPath.item.setGradientBackground 中,如下所示:

I have tried appending each [colour] into an array, then placing that into the .setGradientBackground at the indexPath.item like so:

var colors = [[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.

推荐答案

setGradientBackground() 中读取你的代码,你总是会初始化一个新的 CAGradientLayer 并将它插入到在您的单元格中的最低位置.每次再次调用 setGradientBackground() 时,新的渐变层将位于您已插入的所有其他渐变之下.

Reading your code in setGradientBackground(), you always init a new CAGradientLayer and insert it at the lowest position in your cell. Every time setGradientBackground() gets called again, the new gradient layer will be positioned below all other gradients you already inserted.

尝试从子层中获取一个已经存在的CAGradientLayer并设置新的颜色.

Try to get a already existing CAGradientLayer from sublayers and set the new colors.

    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)

编辑

这是一个更快速的版本(Swift 4.2),使用 compactMap() 来获取 layer.sublayers 中的第一个现有渐变层:

This is a more swift like version (Swift 4.2) using compactMap() for getting the first existing gradient layer in layer.sublayers:

if let existingLayer = (layer.sublayers?.compactMap { $0 as? CAGradientLayer })?.first {
    gradientLayer = existingLayer
}

这篇关于渐变保持在集合视图中的单元格之间快速切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆