CGPathCreateMutable

 
ios 图形与动画学习笔记 构造路径(CGPathCreateMutable)
一系列点放在一起,构成了一个形状。一系列的形状放在一起,构成了一个路径。

/*

     路径属于我们正在绘制他们的上下文。路径没有边界(Boundary)或特定的形状,不想我们使用路径绘制出来的形状。

     但路径没有边界框(Bounding boxes). 此处,Boundary与Bounding boxes完全不一样。

     边界显示你在画布上哪些不可以用来绘画,而路径的边界框是包含了所有路径的形状、点和其他已经绘制的对象的最小矩形。

     

     使用路径创建步骤:创建路径的方法返回一个路径的句柄,可以在绘制图形的使用就可以把句柄作为传递给core Graphics。

                    当创建路径之后,可以向它添加不同的点、线条和形状,之后绘制图形。

     1、CGPathCreateMutable 函数

        创建一个CGMutablePathRef 的可变路径,并返回其句柄。

     2、CGPathMoveToPoint 过程

        在路径上移动当前画笔的位置到一个点,这个点由CGPoint 类型的参数指定。

     3、CGPathAddLineToPoint 过程

        从当前的画笔位置向指定位置(同样由CGPoint类型的值指定)绘制线段

     4、CGContextAddPath 过程

        添加一个由句柄指定的路径的图形上下文,准备用于绘图

     5、CGContextDrawPath 过程

        在图形上下文中绘制给出的路径。

     6、CGPathRelease 过程

        释放为路径句柄分配的内存。

     7、CGPathAddRect 过程

        向路径添加一个矩形。矩形的边界由一个CGRect 结构体指定。

     */

    

    /*

     *创建一个新的可变路径(CGPathCreateMutable),把该路径加到你的图形上下文(CGContextAddPath)

     *并把它绘制到图形上下文中(CGContextDrawPath)

     */


具体代码:
 

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

    /*

     *创建一个新的可变路径(CGPathCreateMutable),把该路径加到你的图形上下文(CGContextAddPath)

     *并把它绘制到图形上下文中(CGContextDrawPath)

     */

    

    /* Create the path */

    CGMutablePathRef path = CGPathCreateMutable();

    

    /* How big is our screen? We want the X to cover the whole screen */

    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    

    /* Start from top-left */

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

    

    /* Draw a line from top-left to bottom-right of the screen */

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

    

    /* Start another line from top-right */

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

    

    /* Draw a line from top-right to bottom-left */

    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 to the context so we can draw it later */

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

    

    /*

     *传入CGPathMoveToPoint等过程的NULL参数代表一个既定的变换,在给定的路径绘制线条时可以使用此变换。

     */

}

转自:http://www.wahenzan.com/a/mdev/ios/2015/0130/1522.html

原文地址:https://www.cnblogs.com/xiaoxiaoyublogs/p/6145041.html