将参数传递给drawRect

时间:2013-06-05 15:45:29

标签: ios uiview drawrect

我有几个UIView类都绘制相同的东西,但使用不同的颜色和alpha设置。我试图传递参数,但无法弄清楚如何将drawRect部分放到我需要的位置。

我是这样画的:

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

我的DrawHexBlue类是这样的:

- (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];
}

我为我需要的每种新颜色和新的alpha值创建一个新类。当然必须有一个更好的方法来使用一个类,只需更改参数/值......?

3 个答案:

答案 0 :(得分:9)

创建单个UIView子类并添加属性:

@interface YourView : UIView

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

@end

然后在drawRect:中,您可以访问此媒体资源:

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

您可以在创建视图时在视图上进行设置:

YourView *hex = [[YourView alloc] initWithFrame:positionFrame];
hex.strokeColor = [UIColor whiteColor];
hex.fillColor = [UIColor blueColor];

答案 1 :(得分:2)

使用属性

绘图的视图应在其头文件中包含以下代码行:

@property (nonatomic, strong) UIColor *colorToDraw;

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

您的init方法应如下所示:

- (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;
}

使用drawRect

- (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];
}

现在,您可以这样绘制:

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

但是,您可能希望将子类的名称更改为不暗示它绘制蓝色的内容。

答案 2 :(得分:2)

创建属性以存储颜色和alpha数据。

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

在此类的init方法中为它们设置一些默认值,例如:

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

然后在drawRect中使用属性而不是硬编码值:

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

然后在视图控制器中,您可以更改它们:

DrawHex *hex = [[DrawHex alloc] initWithFrame:positionFrame];
hex.fillColor = [UIColor redColor];
// you get the idea