【问题标题】:drawRect() draw a custom shape, why can't fill color?drawRect() 绘制自定义形状,为什么不能填充颜色?
【发布时间】:2013-07-16 06:40:33
【问题描述】:

我正在编写一个自定义形状的 HUD 并用一些颜色填充它,但我无法填充我的 HUD 的右侧部分。

#define startX 20
#define startY 20
#define circleDiameter 60
#define PI 3.14159265358979323846

代码如下:

- (void)drawRect:(CGRect)rect

CGContextClearRect(UIGraphicsGetCurrentContext(), rect);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context,self.bounds);

CGContextSetLineWidth(context, 1.0f);
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:255 blue:0 alpha:0.5].CGColor);
CGRect circlePoint = CGRectMake(startX, startY, circleDiameter, circleDiameter);
CGContextFillEllipseInRect(context, circlePoint);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextAddEllipseInRect(context, circlePoint);
CGContextStrokePath(context);

CGContextSetLineWidth(context, 1.0f);

CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);

CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:255 blue:0 alpha:0.5].CGColor);
CGContextBeginPath(context);
//line start x.
CGContextMoveToPoint(context, 70, 28);
CGContextAddLineToPoint(context, 170, 28);
CGContextMoveToPoint(context, 70, 72);
CGContextAddLineToPoint(context, 170, 72);
CGContextStrokePath(context);
//draw radius
CGContextAddArc(context, 170, 50, 22, -PI/2, PI/2, 0);
CGContextStrokePath(context);

CGContextClosePath(context);
CGContextFillPath(context);
}

我想用绿色填充整个路径,我该怎么办?

这是结果快照。:

【问题讨论】:

  • 使用M_PI,而不是#define PI 3.14159265358979323846(显然,不会回答你的问题)
  • 好的,感谢您的关注。
  • UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(55, 28, 140, 44) cornerRadius:22]; CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:255 blue:0 alpha:0.5].CGColor); [bezierPath fill];
  • 试试下面这个CGContextClosePath(context); CGContextFillPath(context);
  • 是的,它奏效了。请回答这个问题,我会采纳为正确答案。非常感谢你。也许它不是最好的。填充的区域比我预期的要大。

标签: ios core-graphics drawrect


【解决方案1】:

使用 BezierPath 绘制带有填充颜色的圆角矩形

UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(55, 28, 140, 44) cornerRadius:22];
 CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:255 blue:0 alpha:0.5].CGColor);
 [bezierPath fill];

【讨论】:

    【解决方案2】:
    CGContextClearRect(UIGraphicsGetCurrentContext(), rect);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
    CGContextFillRect(context,self.bounds);
    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGContextSetAllowsAntialiasing(context, YES);
    CGContextSetShouldAntialias(context, YES);
    
    
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(55, 28, 138, 44) cornerRadius:22]; CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0. green:0.5 blue:0. alpha:0.5].CGColor);
    [bezierPath fill];
    [bezierPath stroke];
    
    
    CGContextSetLineWidth(context, 1.0f);
    
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:0.5 blue:0 alpha:0.5].CGColor);
    CGRect circlePoint = CGRectMake(startX, startY, circleDiameter, circleDiameter);
    CGContextFillEllipseInRect(context, circlePoint);
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextStrokeEllipseInRect(context, circlePoint);
    

    这是我所期望的结果!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 2013-08-27
      • 2012-12-24
      相关资源
      最近更新 更多