CAGradient 使用 UIColor 数组
CAGradient using UIColor array
我将 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] 和 gradientColours1, but any other ones after that don't work as they use 1 and 2, 2 和 [3] 等等...
我觉得这似乎有一个明显的答案,但我暂时想不出来...
感谢您的帮助。
编辑:
图片清晰:
第一个渐变效果很好:
第一个渐变效果很好:
第二个渐变从前一个渐变中获取第一个颜色值:
我可以通过使用简单的 odd/Even 逻辑来解决这个问题,请参阅下面的程序
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
}
我将 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] 和 gradientColours1, but any other ones after that don't work as they use 1 and 2, 2 和 [3] 等等...
我觉得这似乎有一个明显的答案,但我暂时想不出来...
感谢您的帮助。
编辑: 图片清晰:
第一个渐变效果很好:
第一个渐变效果很好:
第二个渐变从前一个渐变中获取第一个颜色值:
我可以通过使用简单的 odd/Even 逻辑来解决这个问题,请参阅下面的程序
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
}