gpt4 book ai didi

ios - 将参数传递给 drawRect

转载 作者:可可西里 更新时间:2023-11-01 03:07:09 26 4
gpt4 key购买 nike

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

最佳答案

创建单个 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];

并且您可以在创建它时在 View 上设置它:

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

关于ios - 将参数传递给 drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16944266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com