I'm passing an RGB color to shapeLayer.fillColor
and shapeLayer.strokeColor
, and my code is crashing. Here's what I tried:
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = (UIColor(red: 57, green: 65, blue: 101, alpha: 1) as! CGColor)
shapeLayer.strokeColor = (UIColor(red: 57, green: 65, blue: 101, alpha: 1) as! CGColor)
If I write my RGB color as:
UIColor(red: 57, green: 65, blue: 101, alpha: 1)
Xcode gives me a warning to change it to:
(UIColor(red: 57, green: 65, blue: 101, alpha: 1) as! CGColor)
When I run Xcode's suggested fix, my app crashes. Why?
First of all the parameters red
,green
, blue
and alpha
must be in range from 0.0 to 1.0
Secondly, you must pass the cgColor
to CGLayer
, e.g.
let color = UIColor(red: 57.0/255.0, green: 65.0/255.0, blue: 101.0/255.0, alpha: 1)
shapeLayer.fillColor = color.cgColor
shapeLayer.strokeColor = color.cgColor