My problem : the color of the fill in square go from black to full blue (255) with no transition color (darkest blue, dark blue, blue...). It seems that the CGContextSetRGBStroke is additive (WTF Oo) . like if the blue was 14 and the next update I put 140 the blue will be 154 and not 140 has I set.
Is someone get that problem ?
in the .h
CGFloat angle;
int width;
int height;
NSTimer* timer;
CGPoint touch;
in the .m
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
angle=0;
width = frame.size.width;
height= frame.size.height;
//self.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:1 alpha:1];
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(update:) userInfo:nil repeats:YES];
}
return self;
}
-(void)update:(NSTimer*)theTimer
{
[self setNeedsDisplay];
}
NS_INLINE CGFloat radians(CGFloat ang)
{
return ang*(M_PI/180.0f);
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx=UIGraphicsGetCurrentContext();
//CGContextSetRGBFillColor(ctx, -1,-1,-1,-1);
CGContextSaveGState(ctx);
CGRect ourRect = CGRectMake(40+angle, 40+angle, 240, 120);
CGFloat colour=(touch.y/768)*255;
NSQLog(@"draw %f",colour);
CGContextSetRGBFillColor(ctx, 0.0,0.0,colour,1);
CGContextClearRect(ctx, rect);
CGContextFillRect(ctx, ourRect);
CGContextSetRGBStrokeColor(ctx, 0.0, 1.0, 0.0, 1.0);
CGContextStrokeRectWithWidth(ctx, ourRect, 10);
CGContextRestoreGState(ctx);
angle+=1;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
touch= [[touches anyObject] locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
touch= [[touches anyObject] locationInView:self];
}