I am new to Swift and I am trying to create a background gradient using CGGradient but I get the following error.
cannot convert value of type int to expected argument type CGGradientDrawingOptions
I do not get why I am not supposed to use an int here. Any help will be appreciated.
override func viewDidLoad() {
super.viewDidLoad()
var currentContext = UIGraphicsGetCurrentContext()
CGContextSaveGState(currentContext);
var colorSpace = CGColorSpaceCreateDeviceRGB()
var topColor = UIColor(red: (57/255.0), green: (150/255.0), blue: (199/255.0), alpha: 1)
var startColorComponents = CGColorGetComponents(topColor.CGColor)
var middleColor = UIColor(red: (90/255.0), green: (60/255.0), blue: (117/255.0), alpha: 0.85)
var middleColorComponents = CGColorGetComponents(middleColor.CGColor)
var bottomColor = UIColor(red: (215/255.0), green: (109/255.0), blue: (109/255.0), alpha: 1)
var endColorComponents = CGColorGetComponents(bottomColor.CGColor)
var colorComponents
= [startColorComponents[0], startColorComponents[1], startColorComponents[2], startColorComponents[3],
middleColorComponents[0], middleColorComponents[1], middleColorComponents[2], middleColorComponents[3],
endColorComponents[0], endColorComponents[1], endColorComponents[2], endColorComponents[3]
]
var locations:[CGFloat] = [0.0, 0.35, 1.0]
var gradient = CGGradientCreateWithColorComponents(colorSpace,&colorComponents,&locations,3)
var startPoint = CGPointMake(-0.3, 0.1)
var endPoint = CGPointMake(0.2, 1.3)
CGContextDrawLinearGradient(currentContext,gradient,startPoint,endPoint, 0)
CGContextRestoreGState(currentContext);
}
I assume you are getting the error on the line that reads:
The problem here is your last parameter where you are passing 0 (an integer).
The declaration for this routine is:
In Objective-C the
CGGradientDrawingOptions
type is an integer-valued thing where 0 means no options. In Swift the drawing options are declared as anenum
of the typeOptionSetType
. The compiler is complaining because it's expecting this enum and not an integer.The way you would specify those options in Swift looks like an array, or set containing elements from the enum. So if you wanted both the
DrawsBeforeStartLocation
option and theDrawsAfterEndLocation
you would write:In your particular case, you don't want either option so just pass an empty option set: