2010-09-30 72 views
0

我的问题:填充正方形的颜色从黑到全蓝(255),没有过渡颜色(最深的蓝色,深蓝色,蓝色...)。看起来CGContextSetRGBStroke是可加的(WTF Oo)。就好像蓝色是14,下一次更新我把140蓝色将是154,而不是我设定的140。CGContextSetRGBFillColor没有设置好颜色

有人得到这个问题吗?

在.M的.H

CGFloat angle; 
    int width; 
    int height; 
    NSTimer* timer; 
    CGPoint touch; 

- (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]; 
    } 

回答

1

只是一个快速的答案,因为我看到了下面几行:

CGFloat colour=(touch.y/768)*255; 
CGContextSetRGBFillColor(ctx, 0.0,0.0,colour,1); 

,则应指定颜色部分为CGFloat,范围从0.0f到1.0f。它可能是你的蓝色部分是在“0到255模式”?

编辑

您的代码来看,我认为你可以通过255颜色计算离开了乘法。当y为0时,你将有0.0f作为蓝色分量,当y是768时,它将是1.0f。

+0

TRUE !!!我正在使用Processing来测试图形代码,并忘记将0-255范围颜色更改为0-1范围。 Thks ... 1天找到它...真诚thk – xeonarno 2010-09-30 16:33:33

+0

如果我的文章有帮助,请注意将其标记为接受的答案? – 2010-09-30 17:10:12