2016-10-15 107 views
2

我在我的視圖中創建了簡單的邊界線。不過,我正在努力爲其提供水平漸變背景色。我應該怎麼做?將水平漸變添加到邊界

我想這樣的,但它只是顏色設置爲白色:

let border = CAGradientLayer() 
    border.frame = CGRect(x: 0, y: self.mainView.frame.height - 2, width: self.mainView.frame.width, height: 2) 
    border.backgroundColor = UIColor.gray.cgColor 
    let color1 = UIColor.black.withAlphaComponent(0.1).cgColor as CGColor 
    let color2 = UIColor.white.withAlphaComponent(0.9).cgColor as CGColor 
    border.locations = [0.60, 1.0]// Are these right coordinates? 
    border.colors = [color2, color1] 

回答

3

我得到它的工作是這樣的:

let gradientLayer = CAGradientLayer() 
     gradientLayer.frame = CGRect(x: 0, y: self.mainView.frame.height - 2, width: self.mainView.frame.width, height: 2) 
     gradientLayer.colors = [UIColor.green,UIColor.blue ].map{$0.cgColor} 
     gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5) 
     gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5) 

而不是使用CALayer()我不得不使用CAGradientLayer()