8

我有几个 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 值创建一个新类。当然必须有一种更好的方法来使用一个类并且只需更改参数/值......?

4

3 回答 3

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];
于 2013-06-05T15:55:30.917 回答
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];

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

于 2013-06-05T15:55:57.327 回答
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
于 2013-06-05T15:56:47.883 回答