私はいくつかの UIView クラスを持っていますが、すべて同じものを描画していますが、色とアルファ設定が異なります。パラメータを渡そうとしましたが、必要な場所に 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];
}
必要な新しい色と新しいアルファ値ごとに新しいクラスを作成します。確かに、1 つのクラスを使用してパラメーター/値を変更するだけのより良い方法が必要です...?