在iPhone中绘制并填充矩形

Pgm*_*eek 3 iphone core-graphics

我有4分说(x1,y1),(x2,y2),(x2,y3)和(x4,y4).我需要绘制一个带有这些点的矩形并在该矩形内填充一种颜色.谁能帮我这个?

Max*_*eod 24

首先,获取当前的图形上下文:

CGContextRef context = UIGraphicsGetCurrentContext();
Run Code Online (Sandbox Code Playgroud)

接下来,定义矩形:

CGRect myRect = {x1, y1, x2 - x1, y2 - y1};
Run Code Online (Sandbox Code Playgroud)

现在,设置填充颜色,例如红色

CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
Run Code Online (Sandbox Code Playgroud)

设置笔触颜色,例如绿色:

CGContextSetRGBStrokeColor(context, 0.0, 1.0, 0.0, 1.0);
Run Code Online (Sandbox Code Playgroud)

最后,填充矩形:

CGContextFillRect(context, myRect);
Run Code Online (Sandbox Code Playgroud)

  • 哇又对这个答案投了两票,再没有评论.不够勇敢提供反馈? (2认同)
  • `CGContextFillRect`不会绘制边框,还需要添加`CGContextStrokeRect`. (2认同)