我有iCarousel設置顯示一系列漸變。CAGradient使用UIColor陣列
// 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]和等等...
我覺得好像這有一個明顯的答案,但我不能在此刻想起來了......
感謝您的幫助。
編輯: 圖像的清晰度:
第一梯度正常工作:
我覺得你錯過了代碼的最後一部分,是剛剛返回frontView的最後一行嗎? –
是的抱歉'返回frontview' – w84u2cy
如果你打印(索引)它會增加預期? –