2017-07-07 79 views
0

我有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,但在這之後的任何其他的,因爲它們使用和122和不工作[3]和等等...

我覺得好像這有一個明顯的答案,但我不能在此刻想起來了......

感謝您的幫助。

編輯: 圖像的清晰度:

第一梯度正常工作:

First gradient, works fine

第一梯度正常工作: 第二梯度需要從以前的梯度第一色值: Second gradient, taking first value from previous gradient

+0

我覺得你錯過了代碼的最後一部分,是剛剛返回frontView的最後一行嗎? –

+0

是的抱歉'返回frontview' – w84u2cy

+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 
    } 
+0

這是如何回答問題的? –

+0

他有漸變色指數的問題,所以我已經解決了索引問題 –