CAGradient使用UIColor数组

时间:2017-07-07 03:25:14

标签: ios swift carousel gradient

我将iCarousel设置为显示一系列渐变。

    // Set gradient colours
    var gradientColours = [

    // First gradient
    UIColor(red:0.33, green:0.38, blue:0.44, alpha:1.0),
    UIColor(red:1.00, green:0.42, blue:0.42, alpha:1.0),

    //Second gradient
    UIColor(red:0.08, green:0.12, blue:0.19, alpha:1.0),
    UIColor(red:0.14, green:0.23, blue:0.33, alpha:1.0),

    // Third gradient
    UIColor(red:0.83, green:0.84, blue:0.16, alpha:1.0),
    UIColor(red:0.83, green:0.84, blue:0.16, alpha:1.0),

    // Fourth gradient
    UIColor(red:0.12, green:0.11, blue:0.09, alpha:1.0),
    UIColor(red:0.56, green:0.05, blue:0.00, alpha:1.0),

    // Fifth gradient
    UIColor(red:0.09, green:0.75, blue:0.99, alpha:1.0),
    UIColor(red:0.80, green:0.19, blue:0.40, alpha:1.0)]

    func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
    let frontView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
    frontView.contentMode = .center

    frontView.layer.cornerRadius = 15;
    frontView.layer.masksToBounds = true;

    let gradient = CAGradientLayer()
    gradient.frame = frontView.bounds
    gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
    gradient.endPoint = CGPoint(x: 1.0, y: 1.0)

    gradient.colors = [gradientColours[index].cgColor, gradientColours[index+1].cgColor]

    frontView.layer.addSublayer(gradient)
    return frontView

问题是,第一个渐变很好,因为它使用了gradientColours [0]和gradientColours 1,但之后的任何其他渐变都不起作用,因为它们使用1和{{ 3}},2和[3]等......

我觉得这有一个明显的答案,但我现在想不到它......

感谢您的帮助。

编辑: 图片清晰:

第一个渐变效果很好:

2

第一个渐变效果很好: 第二个渐变从前一个渐变获取第一个颜色值: First gradient, works fine

1 个答案:

答案 0 :(得分:1)

我通过使用简单的奇数/偶数逻辑来解决这个问题,请参阅以下程序

 import Foundation
var gradientColours = [

    // First gradient
    UIColor(red:0.33, green:0.38, blue:0.44, alpha:1.0),
    UIColor(red:1.00, green:0.42, blue:0.42, alpha:1.0),

    //Second gradient
    UIColor(red:0.08, green:0.12, blue:0.19, alpha:1.0),
    UIColor(red:0.14, green:0.23, blue:0.33, alpha:1.0),

    // Third gradient
    UIColor(red:0.83, green:0.84, blue:0.16, alpha:1.0),
    UIColor(red:0.83, green:0.84, blue:0.16, alpha:1.0),

    // Fourth gradient
    UIColor(red:0.12, green:0.11, blue:0.09, alpha:1.0),
    UIColor(red:0.56, green:0.05, blue:0.00, alpha:1.0),

    // Fifth gradient
    UIColor(red:0.09, green:0.75, blue:0.99, alpha:1.0),
    UIColor(red:0.80, green:0.19, blue:0.40, alpha:1.0)]

       var indexOfGradientColor = 0

     func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView
     {
        let frontView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 200))
        frontView.contentMode = .center

        frontView.layer.cornerRadius = 15;
        frontView.layer.masksToBounds = true;

        let gradient = CAGradientLayer()
        gradient.frame = frontView.bounds
        gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradient.endPoint = CGPoint(x: 1.0, y: 1.0)

        if (indexOfGradientColor%2 == 0)
        {
            gradient.colors = [gradientColours[indexOfGradientColor].cgColor, gradientColours[indexOfGradientColor+1].cgColor]

        }
        else
        { gradient.colors = [gradientColours[indexOfGradientColor+1].cgColor, gradientColours[indexOfGradientColor+2].cgColor]}
        if indexOfGradientColor < (gradientColours.count-2)
        {
                indexOfGradientColor += 2
        }
        else
        {
            indexOfGradientColor = 0
        }

        frontView.layer.addSublayer(gradient)
        return frontView
    }