Ookey Ookey - 1 year ago 140
Swift Question

CAGradientLayer not painting form mask

There is a CAShapeLayer

enter image description here

and when I adding it as a mask I'm getting a blank screen

here is my code:

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)


I don't understand why it's not working.

Answer Source

You've simply forgot that CALayer always uses CGColor.

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

Hope it helps

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download