san*_*ony 8 uiview drawrect ios
我有几个UIView类都绘制相同的东西,但使用不同的颜色和alpha设置.我试图传递参数,但无法弄清楚如何获得我需要的drawRect部分.
我这样画:
CGRect positionFrame = CGRectMake(widthCoordinate,heightCoordinate,20.9f,16.5f);
DrawHexBlue *hex = [[DrawHexBlue alloc] initWithFrame:positionFrame];
[self.imageView addSubview:hex];
Run Code Online (Sandbox Code Playgroud)
我的DrawHexBlue类是这样的:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self)
{
[self setOpaque:YES];
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
Run Code Online (Sandbox Code Playgroud)
}
- (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];
}
Run Code Online (Sandbox Code Playgroud)
我为我需要的每种新颜色和新的alpha值创建了一个新类.当然必须有一个更好的方法来使用一个类,只需更改参数/值......?
创建单个UIView子类并添加属性:
@interface YourView : UIView
@property (nonatomic, strong) UIColor *strokeColor;
@property (nonatomic, strong) UIColor *fillColor;
@end
Run Code Online (Sandbox Code Playgroud)
然后在drawRect:您可以访问此属性:
- (void)drawRect:(CGRect)rect{
UIBezierPath *aPath = [UIBezierPath bezierPath];
[self.strokeColor setStroke];
[self.fillColor setFill];
Run Code Online (Sandbox Code Playgroud)
您可以在创建视图时在视图上进行设置:
YourView *hex = [[YourView alloc] initWithFrame:positionFrame];
hex.strokeColor = [UIColor whiteColor];
hex.fillColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3201 次 |
| 最近记录: |