Drawing Rectangles(绘制矩形)

本文详细介绍了如何使用Core Graphics API在iOS应用中绘制复杂的矩形路径,包括创建路径、设置属性、绘制路径等关键步骤,并通过实例展示了如何在不同上下文中应用这些技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

绘制过程:

    创建路径 -CGMutablePathRef  CGPathCreateMutable (void);  //CGMutablePathRef path = CGPathCreateMutable();

    设置矩形 -CGRect CGRectMake (CGFloat x,CGFloat y,CGFloat width,CGFloat height);

    将所要绘制的图形(矩形)加入路径 

        加入单个矩形 -void  CGPathAddRect (CGMutablePathRef path,const CGAffineTransform *m,CGRect rect); //第二个参数:NULL

        加入多个矩形 -void  CGPathAddRects (CGMutablePathRef path,const CGAffineTransform *m,const CGRect rects[],size_t count);

    创建上下文环境 -CGContextRef  UIGraphicsGetCurrentContext (void); //CGContextRef currentContext = UIGraphicsGetCurrentContext();

        在环境中添加路径 -void  CGContextAddPath (CGContextRef context,CGPathRef path); 

    设置相关属性 - (void)setFill  //填充颜色

               - (void)setStroke  //边界颜色

              - void  CGContextSetLineWidth (CGContextRef c,CGFloat width); //线宽

    绘制路径 -void  CGContextDrawPath (CGContextRef c,CGPathDrawingMode mode);

    释放 -void  CGPathRelease (CGPathRef path);


- (void)drawRect:(CGRect)rect{  

    /* Create the path */

   CGMutablePathRef path = CGPathCreateMutable();

    

   CGRect rectangle = CGRectMake(10.0f,30.0f,200.0f,300.0f);

   CGPathAddRect(path, NULL,rectangle);

    

   // CGRect rectangle1 = CGRectMake(10.0f,30.0f,200.0f,300.0f);

   // CGRect rectangle2 = CGRectMake(40.0f,100.0f,90.0f,300.0f);

   // CGRect rectangles[2] = {rectangle1, rectangle2};

   // CGPathAddRects(path,NULL,(const CGRect *)&rectangles,2);


    /* Get the handle to the current context */

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    

    /* Add the path to the context */

   CGContextAddPath(currentContext, path);

    

    /* Set the fill color to cornflower blue */

    [[UIColor colorWithRed:0.20f

                     green:0.60f

                      blue:0.80f

                     alpha:1.0f]setFill];

    

    /* Set the stroke color to brown */

    [[UIColor brownColor]setStroke];

    

    /* Set the line width (for the stroke) to 5 */

   CGContextSetLineWidth(currentContext,5.0f);

    

    /* Stroke and fill the path on the context */

   CGContextDrawPath(currentContext,kCGPathFillStroke);

    

    /* Dispose of the path */

   CGPathRelease(path);

    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值