gpt4 book ai didi

iOS - CGContextStrokePath/CGContextDrawPath 删除黑色背景颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:00 25 4
gpt4 key购买 nike

我正在尝试绘制一条直线虚线。我已经得到它,所以它绘制了破折号,但这种黑色背景颜色仍然存在。我似乎对其他问题有很多答案,但没有一个有效。

Apple's documentation似乎指向填充颜色并使用 CGContextFillPath 或 CGContextDrawPath,但这些都不起作用,虚线的背景仍然是黑色。

- (void)drawRect:(CGRect)rect {
CGContextRef contextRef = UIGraphicsGetCurrentContext();

GLFloat lines[] = {self.frame.size.width, self.frame.size.width*2};

CGFloat grey[4] = {0.6f, 0.6f, 0.6f, 1.0f};
CGContextSetStrokeColor(contextRef, grey);
CGContextSetFillColorWithColor(contextRef, [[UIColor clearColor] CGColor]);

CGContextSetLineDash(contextRef, 0, lines, 2);
CGContextSetLineWidth(contextRef, self.frame.size.width);

CGContextBeginPath(contextRef);
CGContextMoveToPoint(contextRef, 0, 0);
CGContextAddLineToPoint(contextRef, 0, self.frame.size.height);

CGContextDrawPath(contextRef, kCGPathFillStroke);
}

最佳答案

手动将此 View 添加到 View 层次结构时,您只需确保设置背景颜色即可。例如,如果您使用 initWithFrame 初始化您的 View ,然后在该方法中设置背景颜色:

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setBackgroundColor:[UIColor clearColor]];
}
return self;
}

或者,或者,在添加 View 时,在那里设置背景颜色:

CustomView *customView = [[CustomView alloc] initWithFrame:frame];
customView.backgroundColor = [UIColor clearColor];
[self.view addSubview:customView];

关于iOS - CGContextStrokePath/CGContextDrawPath 删除黑色背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18129716/

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