DDPWNAGE
DDPWNAGE

Reputation: 1443

Array of CGColorRef objects

I'm trying to create a gradient texture in SpriteKit.

To do that, I have to use a CAGradientLayer and render an image. My problem is with setting the colors.

From Apple's Documentation:

An array of CGColorRef objects defining the color of each gradient stop. Animatable.

So in code, I tried this:

gradient.colors = [NSArray arrayWithObjects:[UIColor redColor].CGColor, [UIColor whiteColor].CGColor, [UIColor blueColor].CGColor, nil];

This doesn't work, because CGColorRef is not an object type and cannot be sent to type id. My error is:

Incompatible pointer types sending CGColorRef (aka struct CGColor *) to parameter of type id

How can I create an array of CGColorRef?

Upvotes: 2

Views: 2453

Answers (1)

luk2302
luk2302

Reputation: 57114

Just use an array literal and cast to (id):

gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor whiteColor].CGColor, (id)[UIColor blueColor].CGColor];

enter image description here

Upvotes: 6

Related Questions