0

我的问题:正方形填充的颜色从黑色变为全蓝色(255),没有过渡颜色(最深的蓝色,深蓝色,蓝色......)。似乎 CGContextSetRGBStroke 是相加的 (WTF Oo) 。就像如果蓝色是 14 并且下一次更新我放 140 蓝色将是 154 而不是 140 我设置了。

有人遇到这个问题吗?

在.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]; 
  }
4

1 回答 1

1

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

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

您应该将颜色部分指定为CGFloat0.0f 到 1.0f 的范围。会不会是你的蓝色部分处于“0 到 255 模式”?

编辑

从您的代码来看,我认为您可以在颜色计算中省略乘以 255。当 y 为 0 时,您将有 0.0f 作为蓝色分量,当 y 为 768 时,它将是 1.0f。

于 2010-09-30T13:11:30.520 回答