Passing parameters to drawRect
Asked Answered
C

3

9

I have a couple of UIView classes all drawing the same thing but in different colors and alpha settings. I've tried to pass parameters but cannot figure out how to get the drawRect part where I need to be.

I draw like this:

CGRect positionFrame = CGRectMake(widthCoordinate,heightCoordinate,20.9f,16.5f);
DrawHexBlue *hex = [[DrawHexBlue alloc] initWithFrame:positionFrame];
[self.imageView addSubview:hex];

My DrawHexBlue class is this:

- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self)
{
    [self setOpaque:YES];
    [self setBackgroundColor:[UIColor clearColor]];
}
return self;

}

- (void)drawRect:(CGRect)rect{    
UIBezierPath *aPath = [UIBezierPath bezierPath];
[[UIColor whiteColor] setStroke];
[[UIColor blueColor] setFill];

// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(7, 0)];

// Draw the lines.
[aPath addLineToPoint:CGPointMake(16.2, 0)];
[aPath addLineToPoint:CGPointMake(20.9, 8.7)];
[aPath addLineToPoint:CGPointMake(16.2, 16.5)];
[aPath addLineToPoint:CGPointMake(6.7, 16.5)];
[aPath addLineToPoint:CGPointMake(2.1, 8.7)];
[aPath closePath];

[aPath setLineWidth:1.0f];
[aPath fillWithBlendMode:kCGBlendModeNormal alpha:0.75];
[aPath stroke];
}

I create a new class for every new color I need and new alpha value. Surely there has to be a better way to use one class and just change parameters/values...?

Cassirer answered 5/6, 2013 at 15:45 Comment(0)
F
9

Create a single UIView subclass and add properties:

@interface YourView : UIView

@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, strong) UIColor *fillColor;

@end

Then in drawRect: you can access this property:

- (void)drawRect:(CGRect)rect{    
    UIBezierPath *aPath = [UIBezierPath bezierPath];
    [self.strokeColor setStroke];
    [self.fillColor setFill];

And you can set it on the view when you create it:

YourView *hex = [[YourView alloc] initWithFrame:positionFrame];
hex.strokeColor = [UIColor whiteColor];
hex.fillColor = [UIColor blueColor];
Flews answered 5/6, 2013 at 15:55 Comment(0)
D
3

Yes.

Use properties.

The view that does the drawing should have these lines of code in its header file:

@property (nonatomic, strong) UIColor *colorToDraw;

- (id)initWithFrame:(CGRect)frame color:(UIColor *)color;

And your init method should look like:

- (id)initWithFrame:(CGRect)frame color:(UIColor *)color
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setOpaque:YES];
        [self setBackgroundColor:[UIColor clearColor]];
        self.colorToDraw = color;
    }
    return self;
}

With a drawRect of:

- (void)drawRect:(CGRect)rect{    
    UIBezierPath *aPath = [UIBezierPath bezierPath];
    [[UIColor whiteColor] setStroke];
    [self.colorToDraw setFill];

    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(7, 0)];

// Draw the lines.
    [aPath addLineToPoint:CGPointMake(16.2, 0)];
    [aPath addLineToPoint:CGPointMake(20.9, 8.7)];
    [aPath addLineToPoint:CGPointMake(16.2, 16.5)];
    [aPath addLineToPoint:CGPointMake(6.7, 16.5)];
    [aPath addLineToPoint:CGPointMake(2.1, 8.7)];
    [aPath closePath];
    
    [aPath setLineWidth:1.0f];
    [aPath fillWithBlendMode:kCGBlendModeNormal alpha:0.75];
    [aPath stroke];
}

Now, you can draw like this:

CGRect positionFrame = CGRectMake(widthCoordinate,heightCoordinate,20.9f,16.5f);   
DrawHexBlue *hex = [[DrawHexBlue alloc] initWithFrame:positionFrame color:[UIColor orangeColor]]; //Or whatever.
[self.imageView addSubview:hex];

You might want to change your subclass's name to something that doesn't imply it draws blue, though.

Delvalle answered 5/6, 2013 at 15:55 Comment(0)
C
2

Make properties to store color and alpha data.

@interface DrawHex : UIView
// ...
@property (nonatomic) CGFloat pathAlpha;
@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, strong) UIColor *fillColor;    
@end

Set some default values for them in your init method of this class, for example:

self.fillColor = [UIColor blueColor];
self.strokeColor = [UIColor whiteColor];
self.pathAlpha = 0.75;

Then in drawRect use properties instead of hard-coded values:

[self.strokeColor setStroke];
[self.fillColor setFill];
//...
[aPath fillWithBlendMode:kCGBlendModeNormal alpha:self.pathAlpha];

Then in your view controller you can change them:

DrawHex *hex = [[DrawHex alloc] initWithFrame:positionFrame];
hex.fillColor = [UIColor redColor];
// you get the idea
Chromic answered 5/6, 2013 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.