我想画一条弧线并填充它。第一张图片就是它的样子。我想得到第二张图片的效果。


- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// 必须清空背景色,不然绘制出来的区域之外有黑色背景
[self setBackgroundColor:[UIColor clearColor]];
[self setUserInteractionEnabled:NO];
}
return self;
}
- (void)drawRect:(CGRect)rect {
float x = rect.origin.x;
float y = rect.origin.y;
float w = rect.size.width;
float h = rect.size.height;
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *fullColor = [UIColor whiteColor];
CGContextSetFillColorWithColor(context, fullColor.CGColor);
CGContextSetRGBStrokeColor(context,1,1,1,1);
CGContextMoveToPoint(context,0,h - 22);//圆弧的起始点
CGContextAddQuadCurveToPoint(context, w / 2, h, w, h - 22);
CGContextMoveToPoint(context,0,h - 22);//圆弧的起始点
CGContextAddLineToPoint(context, 0, h);
CGContextAddLineToPoint(context, w, h);
CGContextAddLineToPoint(context, w, h - 22);
CGContextStrokePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
}发布于 2018-09-12 18:54:48
- (void)drawRect:(CGRect)rect {
float x = rect.origin.x;
float y = rect.origin.y;
float w = rect.size.width;
float h = rect.size.height;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, h - 22, kScreenWidth , 22));
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
// This method
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextMoveToPoint(context,0,h - 22);
CGContextAddQuadCurveToPoint(context, w / 2, h + 14, w, h - 22);
CGContextDrawPath(context, kCGPathFill);
// This method
CGContextSetBlendMode(context, kCGBlendModeNormal);
}我找到了一个解决方案。
发布于 2018-09-12 21:03:03
CGContextStrokePath()清除当前路径。这也适用于CGContextDrawPath()、CGContextEOFillPath()和CGContextFillPath()。
因此,当调用CGContextDrawPath(context, kCGPathFillStroke);时,上下文中的当前路径刚刚被清除,它是一个空路径,不会绘制任何内容。
删除上面的CGContextStrokePath(context);行应该可以解决您的问题。
https://stackoverflow.com/questions/52292526
复制相似问题