美文网首页
ios 图形与动画学习笔记 构造路径

ios 图形与动画学习笔记 构造路径

作者: 默默_David | 来源:发表于2017-04-24 19:53 被阅读50次

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 */

CGMutablePathRefpath =CGPathCreateMutable();

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

CGRectscreenBounds = [[UIScreenmainScreen]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 */

CGContextRefcurrentContext =UIGraphicsGetCurrentContext();

/* Add the path to the context so we can draw it later */

CGContextAddPath(currentContext, path);

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

[[UIColorblueColor]setStroke];

/* Draw the path with stroke color */

CGContextDrawPath(currentContext,kCGPathStroke);

/* Finally release the path object */

CGPathRelease(path);

/*

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

*/

}

相关文章

网友评论

      本文标题:ios 图形与动画学习笔记 构造路径

      本文链接:https://www.haomeiwen.com/subject/abopzttx.html