Toncean Cosmin
Toncean Cosmin

Reputation: 535

Core Graphics Color Context

So ... I have this app that draws shapes and then the user can color them. I made a tabbar that has 4 shapes and when one is clicked the respective shape is drawn. I took the drawing idea from the quartzDemo. So I have a shape general class and then shape_name subclasses of shape. Here is the code for the 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

When a shape is tapped a color menu appears and I want to change the color when a button from the menu is tapped. How can I do this.

Ty in advance.

Upvotes: 1

Views: 2885

Answers (1)

Costique
Costique

Reputation: 23722

I suggest adding a property to your base shape class like this:

@property(nonatomic,retain) UIColor* fillColor;

In your menu implementation set the value of the property to a UIColor object of your user's choice and send the shape setNeedsDisplay. Then modify your drawInContext: method as follows:

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

Upvotes: 4

Related Questions