核心图形颜色上下文

问题描述:

所以...我有这个应用程序绘制形状,然后用户可以为它们着色。我做了一个有4个形状的tabbar,当点击一个时,相应的形状被绘制出来。我从quartzDemo中获取了绘图的想法。所以我有一个形状通用类,然后shape_name的形状子类。这是Square的代码。核心图形颜色上下文

@implementation Square 

- (void)drawInContext:(CGContextRef)context { 
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0); 
    CGContextSetLineWidth(context, 1.0);  
    CGContextBeginPath(context); 
    CGContextMoveToPoint(context, 1, 1); 
    CGContextAddLineToPoint(context, 1, 79); 
    CGContextAddLineToPoint(context, 79, 79); 
    CGContextAddLineToPoint(context, 79, 1); 
    CGContextAddLineToPoint(context, 1, 1); 
    CGContextClosePath(context); 
    CGContextStrokePath(context); 

} 

@end 

当形状被窃听的彩色菜单出现,我想改变颜色时,从菜单按钮被窃听。我怎样才能做到这一点。

Ty提前。

我建议增加一个属性到您的基本形状类是这样的:

@property(nonatomic,retain) UIColor* fillColor; 

在你的菜单中执行属性的值设置为您的用户选择的对象的UIColor和发送形状setNeedsDisplay。然后修改您的drawInContext:方法如下:

CGContextSetFillColorWithColor(context, self.fillColor.CGColor); 
// ... your current drawing code goes here 
// replace the last line, with CGContextStrokePath(), with this: 
CGContextDrawPath(context, kCGPathFillStroke); 
+0

那样做了......谢谢 – 2011-02-09 11:20:59