gpt4 book ai didi

ios - CAGradient 使用 UIColor 数组

转载 作者:行者123 更新时间:2023-11-28 15:28:08 24 4
gpt4 key购买 nike

我将 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 , 但之后的任何其他人都无法使用 12 , 2和 [3] 等等......

我觉得这似乎有一个明显的答案,但我暂时想不起来......

感谢您的帮助。

编辑:图片清晰:

第一个渐变效果很好:

First gradient, works fine

第一个渐变效果很好:第二个渐变从前一个渐变中获取第一个颜色值: Second gradient, taking first value from previous gradient

最佳答案

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

 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
}

关于ios - CAGradient 使用 UIColor 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44962061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com