Reputation: 59
I have iCarousel set up to display a series of gradients.
// 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
The problem is, the first gradient is fine as it uses gradientColours[0] and gradientColours1, but any other ones after that don't work as they use 1 and 2, 2 and [3] and so on...
I feel as though this has an obvious answer but I cannot think of it at the moment...
Thanks for the help.
Edit: Images for clarity:
First gradient works fine:
First gradient works fine:
Second gradient takes first colour value from previous gradient:
Upvotes: 1
Views: 306
Reputation: 4470
I have solution for this by using simple odd/Even Logic see the below program
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
}
Upvotes: 2