SYNT4X
SYNT4X

Reputation: 210

CGContextDrawRadialGradient in 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)
}

Upvotes: 3

Views: 2377

Answers (1)

Martin R
Martin R

Reputation: 539735

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.

Upvotes: 1

Related Questions