2017-08-16 170 views
0

有一個CAShapeLayerCAGradientLayer不畫形式掩蓋

enter image description here

,當我將它作爲一個面具我得到一個空白屏幕

這裏是我的代碼:

let width: CGFloat = frame.width 
    let radius: CGFloat = (width - 35)/2 

    let poing = CGPoint(x: width/2, y: radius + 8) 
    let path = UIBezierPath(arcCenter: poing, radius: (width - 35)/2, 
         startAngle: CGFloat(180).toRadians(), 
         endAngle: CGFloat(360).toRadians(), 
         clockwise: true) 

    let mask = CAShapeLayer() 
    mask.path = path.cgPath 

    mask.lineWidth = 8 
    mask.strokeStart = 0 
    mask.strokeEnd = 1 
    mask.lineCap = kCALineCapRound 
    mask.fillColor = UIColor.clear.cgColor 
    mask.strokeColor = progressColor.cgColor 

    let gradient = CAGradientLayer() 
    gradient.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height) 
    gradient.startPoint = CGPoint(x: 0, y: 1) 
    gradient.endPoint = CGPoint(x: 1, y: 1) 
    gradient.colors = [UIColor.white, UIColor.blue] 

    gradient.mask = mask 

    layer.addSublayer(gradient) 

我不明白爲什麼它不起作用。

回答

1

你簡直忘了CALayer總是使用CGColor。

gradient.colors = [UIColor.white.cgColor, UIColor.blue.cgColor] 

希望它可以幫助