Constructing Paths(构建路径)

本文介绍如何使用CGPath绘制路径,包括创建路径、设置起点和终点、添加路径到上下文环境等步骤。通过实例展示了如何利用CGPath绘制屏幕对角线,并设置了绘制的颜色和模式。

points -> a shape:多个点构成一个形状

shapes -> a path:多个形状构成一个路径

绘制过程:

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

    画线条,并将其加入路径

          设置起点 -void  CGPathMoveToPoint (CGMutablePathRef path,const CGAffineTransform *m,CGFloat x,CGFloat y); //第二个参数:NULL

          设置终点 -void  CGPathAddLineToPoint (CGMutablePathRef path,const CGAffineTransform *m,CGFloat x,CGFloat y);

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

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

    设置相关属性 - (void)setStroke  //颜色

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

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

    释放 -void  CGPathRelease (CGPathRef path);


    重要的3种绘制模式 -CGPathDrawingMode :kCGPathFill, kCGPathStroke, kCGPathFillStroke  ; 

- (void)drawRect:(CGRect)rect{

    

    /* Create the path */

   CGMutablePathRef path = CGPathCreateMutable();

    

   CGRect screenBounds = [[UIScreen mainScreen] bounds];

    

    /* Start:左上 */

   CGPathMoveToPoint(path, NULL, screenBounds.origin.x,screenBounds.origin.y);

    

    /* Draw a line : 从左上到右下 */

   CGPathAddLineToPoint(path,NULL,screenBounds.size.width,screenBounds.size.height);

    

    /* Start another line:右上 */

    CGPathMoveToPoint(path,NULL,screenBounds.size.width,screenBounds.origin.y);

    

    /* Draw a line :从右上到左下 */

    CGPathAddLineToPoint(path,NULL,screenBounds.origin.x,screenBounds.size.height);

    

    /* Get the context that the path has to be drawn on */

   CGContextRef currentContext = UIGraphicsGetCurrentContext();

    

    /* Add the path*/

   CGContextAddPath(currentContext,path);

    

    /* Set the blue color as the stroke color */

    [[UIColor blueColor] setStroke];

    

    /* Draw the path with stroke color */

   CGContextDrawPath(currentContext,kCGPathStroke);

    

    /* Finally release the path object */

   CGPathRelease(path);  

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值