Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGContextDrawRadialGradient in Swift

Tags:

xcode

ios

swift

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)
}
like image 494
SYNT4X Avatar asked Mar 15 '26 12:03

SYNT4X


1 Answers

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.

like image 155
Martin R Avatar answered Mar 18 '26 02:03

Martin R