Reputation: 20835
I have a UIView & I want that view's layer to be a continuously looping animation but I'm having some trouble getting started. I have the following in my view subclass:
+ (Class)layerClass {
return [CALayer class];
}
Then in my viewController I have:
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],nil]];
[[[self view] layer] addSublayer:gradient];
This crashes my app. What exactly am I doing wrong? The app crashes w/ EXC_BAD_ACCESS.
Upvotes: 0
Views: 778
Reputation: 18428
You forget to specify the locations for colors. By default, the location is nil.
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:
[[UIColor redColor] CGColor],
[[UIColor blueColor] CGColor],
nil]];
[gradient setLocations:
[NSNumber numberWithFloat:0.0], // for redColor
[NSNumber numberWithFloat:1.0], // for blueColor
nil]];
[[[self view] layer] addSublayer:gradient];
Upvotes: 0
Reputation: 24486
Actually the crash you are having isn't related to not setting locations. The issue has to do with the fact that you're passing the wrong types in your setColors array. A CGColorRef is the types that is expected. Your code should look like this:
CAGradientLayer *gradient = [CAGradientLayer layer];
[gradient setColors:[NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor],
(id)[[UIColor blueColor] CGColor],nil]];
[[[self view] layer] addSublayer:gradient];
The locations parameter of a CAGradientLayer is optional.
Best regards.
Upvotes: 1