Search code examples
iosobjective-cxcodeswiftcagradientlayer

CAGradientLayer with r g b values not working SWIFT


I am trying to add a CAGradientLayer and I am new to swift. I have the below code but keep getting a error saying 'expected identifier' but I can't seem to resolve it

var gradient: CAGradientLayer = CAGradientLayer()
    gradient.frame = view.bounds
    gradient.colors = [UIColor( red:0.388 green:0.412 blue:1.000 alpha:1.000).CGColor,UIColor( red:1.000 green:0.565 blue:0.380 alpha:1.000).CGColor,UIColor( red:0.953 green:0.102 blue:1.000 alpha:1.000).CGColor]
    gradient.startPoint = CGPoint(x: 0.00, y: 0.00)
    gradient.endPoint = CGPoint(x: 1.00, y: 1.00)
    view.layer.insertSublayer(gradient, atIndex: 0)

Solution

  • In Swift all method parameters (unlike Objective-C) must be separated by a comma. So add all the comma separators between the color components in UIColor, for example

    UIColor(red:0.388, green:0.412, blue:1.000, alpha:1.000).CGColor