8

私はいくつかの 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 つのクラスを使用してパラメーター/値を変更するだけのより良い方法が必要です...?

4

3 に答える 3

9

UIViewサブクラスを1 つ作成し、プロパティを追加します。

@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

色とアルファ データを格納するプロパティを作成します。

@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];

次に、View Controller でそれらを変更できます。

DrawHex *hex = [[DrawHex alloc] initWithFrame:positionFrame];
hex.fillColor = [UIColor redColor];
// you get the idea
于 2013-06-05T15:56:47.883 に答える
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 に答える