I tried to draw a Gradient Dot, but the CGGradientCreateWithColors always returns nil. I am new to Swift and Objective C (started today, but made some Apps with Xamarin). Can please anyone explain what is wrong with the following code?
func initFields(){
var startColor = UIColor.blueColor().CGColor
var endColor = UIColor.greenColor().CGColor
var colors: CFArray = [startColor, endColor]
Gradient = CGGradientCreateWithColors(CGColorSpaceCreateDeviceRGB(), colors, [0, 10])
}
func DrawSomething(){
UIGraphicsBeginImageContext(frame.size)
CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(), Gradient!, TouchLocation!, 10, TouchLocation!, 10, 0)
}
From the CGGradientCreateWithColors() documentation:
... each location must be a CGFloat value in the range of 0 to 1, inclusive.
So change [0, 10] to [0, 1] in the creation of the gradient.
The color at location 0 is mapped to the starting circle and the color at location 1 is mapped to the ending circle.
It also seems strange that the starting circle and the ending circle are identical in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With