iOS CGContextRef填充颜色

时间:2018-09-12 10:02:11

标签: ios objective-c iphone

我想画一个圆弧并填充它。第一张照片就是它。我想得到第二张照片的效果

The current

I want

- (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);
}

2 个答案:

答案 0 :(得分:1)

- (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);
  }

我找到了解决方法。

答案 1 :(得分:0)

0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /bin/bash -l -c 'cd /var/www/example.com && curl -i https://www.example.com/ >> log/curl-www.example.com.log 2>&1' 清除当前路径。这也适用于CGContextStrokePath()CGContextDrawPath()CGContextEOFillPath()

因此,在进行调用CGContextFillPath()时,上下文中的当前路径刚刚被清除,并且是空路径,没有任何内容。

删除上面的行CGContextDrawPath(context, kCGPathFillStroke);应该可以解决您的问题。