Here is a very quick class I wrote based on an idea from @nsxdavid (link). Nothing impressive, but hope it helps.
It is a subclass of CCColorLayer and draws simple two color gradients.
Here is the code:
CCGradientLayer.h
#import "ccTypes.h"
#import "CCLayer.h"
#import "CCDirector.h"
@interface CCGradientLayer : CCColorLayer {
ccColor4B top;
ccColor4B bottom;
}
- (id)initWithTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor width:(GLfloat)w height:(GLfloat)h;
- (id)initWithTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor;
+ (id)layerWithTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor;
+ (id)layerWithTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor width:(GLfloat)w height:(GLfloat)h;
- (void)setTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor;
- (void)updateColor;
@end
CCGradientLayer.m
#import "CCGradientLayer.h"
@implementation CCGradientLayer
- (id)initWithTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor {
CGSize s = [CCDirector sharedDirector].winSize;
return [self initWithTopColor:topColor bottomColor:bottomColor width:s.width height:s.height];
}
+ (id)layerWithTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor {
return [[[self alloc] initWithTopColor:topColor bottomColor:bottomColor] autorelease];
}
+ (id)layerWithTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor width:(GLfloat)w height:(GLfloat)h {
return [[[self alloc] initWithTopColor:topColor bottomColor:bottomColor width:w height:h] autorelease];
}
- (id)initWithTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor width:(GLfloat)w height:(GLfloat)h {
if ((self = [super initWithColor:topColor width:w height:h])) {
top = topColor;
bottom = bottomColor;
[self updateColor];
}
return self;
}
- (void)setTopColor:(ccColor4B)topColor bottomColor:(ccColor4B)bottomColor {
top = topColor;
bottomColor = bottomColor;
[self updateColor];
}
- (void) updateColor {
for( NSUInteger i=0; i < 16; i++)
{
switch (i) {
case 0:
squareColors[0] = bottom.r;
break;
case 1:
squareColors[1] = bottom.g;
break;
case 2:
squareColors[2] = bottom.b;
break;
case 3:
squareColors[3] = bottom.a;
break;
case 4:
squareColors[4] = bottom.r;
break;
case 5:
squareColors[5] = bottom.g;
break;
case 6:
squareColors[6] = bottom.b;
break;
case 7:
squareColors[7] = bottom.a;
break;
case 8:
squareColors[8] = top.r;
break;
case 9:
squareColors[9] = top.g;
break;
case 10:
squareColors[10] = top.b;
break;
case 11:
squareColors[11] = top.a;
break;
case 12:
squareColors[12] = top.r;
break;
case 13:
squareColors[13] = top.g;
break;
case 14:
squareColors[14] = top.b;
break;
case 15:
squareColors[15] = top.a;
break;
default:
break;
}
}
}
@end
Obviously, the updateColors method could be optimized, but it ran quite fast in my tests and quite honestly I am struggling to wrap my head around the modulus operator :P.