2015-02-10 34 views
0

我嘗試使用操場製作漸變,然後將其變爲uiview的子類以在我的代碼庫中使用它。不同於模擬器的操場中的漸變

操場代碼是這樣的:

let view = UIView(frame: CGRect(x: 0, y: 0, width: 640, height: 41 
    )) 

let gradient: CAGradientLayer = CAGradientLayer(layer: view.layer) 
gradient.frame = view.frame 

let lightColor = UIColor(red: 157/256, green: 157/256, blue: 154/256, alpha: 1) 
let darkColor = UIColor(red: 108/256, green: 104/256, blue: 104/256, alpha: 1) 

gradient.colors = [lightColor.CGColor, darkColor.CGColor] 
gradient.locations = [0.6 , 0.4] 
gradient.startPoint = CGPoint(x: 1, y: 0.55) 
gradient.endPoint = CGPoint(x: 0, y: 0.45) 
view.layer.insertSublayer(gradient, atIndex: 0) 
view //displayed 

,並顯示如下:

我試圖把它做成我的基本代碼:

class GradiantView : UIView { 
    override var frame: CGRect { 
     didSet { 
      let gradient: CAGradientLayer = CAGradientLayer(layer: layer) 
      gradient.frame = bounds 

      let lightColor = UIColor(red: 157/256, green: 157/256, blue: 154/256, alpha: 1) 
      let darkColor = UIColor(red: 108/256, green: 104/256, blue: 104/256, alpha: 1) 

      gradient.colors = [lightColor.CGColor, darkColor.CGColor] 
      gradient.locations = [0.6 , 0.4] 
      gradient.startPoint = CGPoint(x: 1, y: 0.55) 
      gradient.endPoint = CGPoint(x: 0, y: 0.45) 
      layer.insertSublayer(gradient, atIndex: 0) 
     } 
    } 
} 

然而,結果是這樣的:(沒有應用灰度)

發生了什麼,以及如何解決它?

回答

0

我倒過來的位置,它似乎工作。 我歡迎任何解釋模擬器和操場之間的區別。

 gradientLayer.locations = [0.4 , 0.6] 
相關問題