2010-11-08 93 views
1

我想知道是否可以使用CoreGraphics绘制的路径的填充颜色动画? 我正在绘制: Simple drawing with Quartz - Core Graphics如何在drawRect中为使用CGContextDrawPath等绘制的路径填充颜色?

我想将其填充颜色从白色改为灰色。 这可能吗?

我知道view.layer.content属性的存在,但在这里有用吗?虽然,我不确定在这种情况下如何使用它。

在此先感谢。

更新

我想这种方式(其越野车,所以我可以告诉大家,如果它去上班) 基本上我创建一个CGImageRef,并把它传递给self.layer.contents这是动画 使用UIView动画,但....我得到奇怪的结果,除了不是动画。

int bitsPerComponent = 8; 
int channels = 4; 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
void *data = malloc(self.bounds.size.width*self.bounds.size.height*channels); 

CGContextRef context = CGBitmapContextCreate(data,      //pointer to data 
              self.bounds.size.width, //width 
              self.bounds.size.height, //height 
              bitsPerComponent,   //bitsPerComponent 
              self.bounds.size.width*channels,//bytesPerRow 
              colorSpace,     //colorSpace 
              kCGImageAlphaPremultipliedFirst); //bitmapInfo 

//method that uses below link's code 
[self _drawBackgroundInContext:context color:UIColorFromMandalaBoxType(type)]; 

CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL,   //info, NULL 
                   data,   //pointer to data 
                   self.bounds.size.width*self.bounds.size.height*channels, //number of bytes 
                   NULL);  //release callback 


CGImageRef image = CGImageCreate(self.bounds.size.width,   //width 
           self.bounds.size.height,   //height 
           bitsPerComponent,    //bitsPerComponent 
           bitsPerComponent*channels,  //bitsPerPixel 
           self.bounds.size.width*channels, //bytesPerRow 
           colorSpace,      //colorSpace 
           kCGImageAlphaPremultipliedFirst, //bitmapInfo 
           dataProvider, NULL, false, kCGRenderingIntentDefault); 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:1.5f]; 
self.layer.contents = (id)image; 
[UIView commitAnimations]; 

CGImageRelease(image); 
CGDataProviderRelease(dataProvider); 
CGContextRelease(context); 
free(data); 
CGColorSpaceRelease(colorSpace); 

是逻辑好吗?,这里的错误在哪里? (

回答

7

编辑答案:
不能在-drawRect:动画的东西,如果你想动画的形状的颜色,你可能想看看CAShapeLayer它允许你指定一个CGPathRef。它应该画,以及中风/填充颜色,所有这些属性都是可以动画的。但是要记住的一点是,这在工作中效率不高。如果你的形状始终不是动画,你可能想要设置shouldRasterizeYES,只要图层没有动画效果,这会显着提高渲染速度,如果你这样做,请小心,因为有shouldRasterize的错误。 opacity为0,并且稍后将其设为非0,shouldRasterize倾向于不绘制任何东西,因为它在0不透明度处缓存绘图。

原来的答复
如果你生活在CoreGraphics中完全,你可以使用CGContextSetFillColorWithColor(),给它的背景和一个CGColorRef(如CGContextSetFillColorWithColor(ctx, [UIColor grayColor].CGColor)如果你想调整的“当前填充颜色。 “图形上下文,你也可以只使用稍微简单[[UIColor grayColor] setFill]

编辑:看你的链接后,你只需要改变,说

CGContextSetFillColorWithColor(context, [UIColor white].CGColor); 

使用任何你想要的颜色。在这种情况下,它将是

CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor); 
+0

那该怎么做动画? – Eiko 2010-11-08 23:47:53

+0

哦,对不起,要么我误读了你的问题,要么以后编辑过。我没有意识到你正在尝试制作动画。我会编辑我的答案以包含一个动画版本。 – 2010-11-09 02:31:51

0

我不认为你可以。石英是为了绘制像按钮这样的静态事物,它不擅长实时绘图。

+0

是的,这就是为什么我要设置layer.contents这是动画;) – nacho4d 2010-11-09 00:44:46

0

你可能会看CALayer和核心动画。你也许可以做到像这样(未经):

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration: 0.8]; 
self.view.layer.backgroundColor = [UIColor grayColor]; 
[UIView commitAnimations]; 

CALayer's backgroundColor为“动画”。这可能需要对drawRect:方法进行一些修改 - 就像使用[UIColor clearColor]进行填充和/或将视图的不透明属性设置为NO等。

只是一个想法 - 但它应该是可行的。

我不得不做类似的事情,但更多的参与。为此,我最终使用了第二个线程和一种生产者/消费者模式。我认为这会损害您的需求,但这是另一种选择。

+0

这不是我的情况,因为我的背景是clearColor。我正在绘制一些CGPaths,我希望这些CGPath的填充颜色是动画而不是图层的背景颜色本身 – nacho4d 2010-11-09 00:47:07

+0

那么我想你可以添加某种NSTimer,它会在你的UIView上设置一些'填充颜色'ivar并强制重绘。然后你的填充矩形电话会使用那个伊娃... – westsider 2010-11-09 00:50:21

+0

我的意思是说,填充色伊娃可以在步骤中改变,以产生动画效果。 – westsider 2010-11-09 05:23:24