かなり面倒くさかったので、
#import "Paint.h"
@implementation Paint
{
CAGradientLayer *grad;
}
-(id)init
{
self = [super init];
grad = [CAGradientLayer layer];
return self;
}
-(void)gradWith:(UIView *)target from:(UIColor *)c1 to:(UIColor *)c2
{
// if grad already exist then remove this.
if (grad){
[grad removeFromSuperlayer];
}
grad.frame = target.bounds;
grad.colors = [NSArray arrayWithObjects:
(id)c1.CGColor,
(id)c2.CGColor,
nil];
[target.layer insertSublayer:grad atIndex:0];
}
-(void)gradWith:(UIView *)target by:(NSMutableArray *)colors
{
if (grad){
[grad removeFromSuperlayer];
}
grad.frame = target.bounds;
NSMutableArray *res = [NSMutableArray array];
for (id color in (NSMutableArray *)colors){
[res addObject:(id)[color CGColor]];
}
grad.colors = [NSArray arrayWithArray:res];
[target.layer insertSublayer:grad atIndex:0];
}
こんなふうなクラスを書いて、
[paint gradWith:_paintedLabel by:[NSArray arrayWithObjects:[UIColor redColor],[UIColor yellowColor], nil]];
[paint gradWith:_paintedLabel from:[UIColor colorWithRed:0.0 green:0.3 blue:1.0 alpha:1.0] to:[UIColor colorWithRed:0.0 green:0.7 blue:0.933 alpha:1.0]];
こんなふうに呼び出すようにしてみた。やっつけだけど、頑張って働いてくれています。