#import "DrawView.h"

@implementation DrawView

/**
* 作用:专门用来绘图
* 什么时候调用:当View显示的时候调用
* @param rect:当View的bounds
*/
- (void)drawRect:(CGRect)rect {
// Drawing code
// NSLog(@"%s",__func__);
// NSLog(@"%@",NSStringFromCGRect(rect)); //1.在drawRect方法当中系统已经帮你创建一个跟View相关联的上下文.(Layer上下文)
//只要获取上下文就行了. //绘制曲线
//1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
UIBezierPath *path = [UIBezierPath bezierPath];
//画曲线
//2.1设置起点
[path moveToPoint:CGPointMake(, )];
//2.2添加一根曲线到某一个点
[path addQuadCurveToPoint:CGPointMake(, ) controlPoint:CGPointMake(, )];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容渲染View上
CGContextStrokePath(ctx); } //画直线
- (void)drawLine{ //1.获取上下文(获取,创建上下文 都 以uigraphics开头)
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.绘制路径
UIBezierPath *path = [UIBezierPath bezierPath];
//2.1 设置起点
[path moveToPoint:CGPointMake(, )];
//2.2 添加一根线到终点
[path addLineToPoint:CGPointMake(, )]; //画第二条线
// [path moveToPoint:CGPointMake(100, 280)];
// [path addLineToPoint:CGPointMake(250, 100)]; //addLineToPoint:把上一条线的终点当作是下一条线的起点
[path addLineToPoint:CGPointMake(, )]; //上下文的状态
//设置线的宽度
CGContextSetLineWidth(ctx, );
//设置线的连接样式(两条线的交点为圆角)
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//设置线的顶角样式(两根线的两端为圆角)
CGContextSetLineCap(ctx, kCGLineCapRound); //设置颜色(此方法会自动判断是stroke描点,还是fill填充而设置颜色)
[[UIColor redColor] set]; //3.把绘制的内容添加到上下文当中.
//UIBezierPath:UIKit框架 -> CGPathRef:CoreGraphics框架
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容显示到View上(渲染到View的layer)(stroke fill)
CGContextStrokePath(ctx); } @end
#import "DrawView.h"

@implementation DrawView

-(void)awakeFromNib{

    //画椭圆
//UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 50)]; //使用UIBezierPath提供的绘图方法进行绘制
//[path stroke]方法中默认做了:1.获取上下文->2.描述路径,3.把路径添加到上下文,4.把上下文的内容渲染View上,只有在drawRect:方法当中才能够获取上下文.在awakeFromNib当获取不到上下文,所以没有办法 进行绘图.
//[path stroke]; } - (void)drawRect:(CGRect)rect { //// 1:绘制矩形 和 直线
// UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
// [path moveToPoint: CGPointMake(50, 250)];
// [path addLineToPoint:CGPointMake(250, 250)];
//
// [path setLineWidth:10];
// [path stroke]; // 2: 使用UIBezierPath提供的绘图方法进行绘制
// 画椭圆或是圆形:当宽高不相等时时椭圆,相等的时候是圆形,调用UIBezierPath的对象方法[path stroke],默认做了1.获取上下文->2.描述路径,3.把路径添加到上下文,4.把上下文的内容渲染View上,只有在drawRect:方法当中才能够获取上下文.在awakeFromNib当获取不到上下文,所以没有办法 进行绘图. // UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
//
// [[UIColor redColor] set];
// [path stroke]; //3:画弧
//Center:弧所在的圆心
//radius:圆的半径
//startAngle:开始角度
//endAngle:截至角度
//clockwise: YES:顺时针 NO:逆时针 NSLog(@"%@",NSStringFromCGPoint(self.center)); //画弧
// CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
// CGFloat radius = rect.size.width * 0.5 - 10;
// //不能直接会用self.center ,是因为self.center坐标是相对于它的父控件.
// UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:-M_PI_2 clockwise:NO];
// [path stroke]; // //画扇形
//
// CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
// CGFloat radius = rect.size.width * 0.5 - 10;
// //不能直接会用self.center ,是因为self.center坐标是相对于它的父控件.
// UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:-M_PI_2 clockwise:NO];
//
// //添加一根线到圆心
// [path addLineToPoint:center];
// [[UIColor redColor] set];
// //关闭路径closePath:从路径终点连接一根线到路径的起点
//// [path closePath];
//
//
//
// //画扇形
// //fill(填充之前,会自动关闭路径)
// [path fill];
//
// //[path stroke];//1.获取上下文->2.描述路径,3.把路径添加到上下文,4.把上下文的内容渲染View上
//
[self drawRect]; } //画矩形
- (void)drawRect{ //1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
//画矩形
//UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
//圆角矩形
//cornerRadius:圆角半径
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(, , , ) cornerRadius:]; [[UIColor yellowColor] set];
//3.把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的内容渲染View上
//CGContextStrokePath(ctx);一个圆圈
CGContextFillPath(ctx);//填充 } @end

1.DrawRect方法作用?什么时候调用.

DrawRect作用:专用在这个方法当中绘图的.只有在这个方法当中才能取得跟View相关联的上下文.

DrawRect是系统自己调用的, 它是当View显示的时候自动调用.

2.画线(基本步骤描述)

2.1获取跟View相关联的上下文

CGContextRef ctx = UIGraphicsGetCurrentContext();

2.2绘制路径

UIBezierPath *path = [UIBezierPath bezierPath];

2.2.1设置起点

[path moveToPoint:CGPointMake(10, 125)];

2.2.2添加一根线到某个点

[path addLineToPoint:CGPointMake(200, 125)];

2.3把路径添加到上下文

CGContextAddPath(ctx,path.CGPath);

2.4把上下文的内容渲染到View上面.

CGContextStrokePath(ctx);

3. 想要再添加一根线怎么办?

第一种方法:重新设置起点,添加一根线到某个点,一个UIBezierPath路径上面可以有多条线.

第二种方法:直接在原来的基础上添加线.把上一条的终点当做下一条线的起点.添加一根线到某个点

直接在下面addLineToPoint:

4.怎么样设置线的宽度,颜色,样式?

设置这些样式,我们称为是修改图形上下文的状态.

设置线宽:CGContextSetLineWidth(ctx, 20);

设置线段的连接样式: CGContextSetLineJoin(ctx, kCGLineJoinRound);

添加顶角样式:CGContextSetLineCap(ctx, kCGLineCapRound);

设置线的颜色: [[UIColor redColor] set];

5.如何画曲线?

画曲线方法比较特殊需要一个控制点来决定曲线的弯曲程度.画曲线方法为:

先设置一个曲线的起点

[path moveToPoint:CGPointMake(10, 125)];

再添加到个点到曲线的终点.同时还须要一个controlPoint(控件点决定曲线弯曲的方法程序)

[path addQuadCurveToPoint:CGPointMake(240, 125) controlPoint:CGPointMake(125, 10)];

6.如何画矩形,圆角矩形?

圆角矩形的画法多了一个参数,cornerRadius

cornerRadius它是矩形的圆角半径.

通过圆角矩形可以画一个圆.当矩形是正方形的时候,把圆角半径设为宽度的一半,就是一个圆.

bezierPathWithRoundedRect:CGRectMake(10, 100, 50, 50) cornerRadius:10

7.如果画椭圆,圆?

画椭圆的方法为:

前两个参数分别代码圆的圆心,后面两个参数分别代表圆的宽度,与高度.

宽高都相等时,画的是一个正圆, 不相等时画的是一个椭圆

bezierPathWithOvalInRect:CGRectMake(10, 100, 50, 50)

8.如何利用UIKit封装的上下文进行画图?

直接来个:[path stroke]就可以了.

它底层的实现,就是获取上下文,拼接路径,把路径添加到上下文,渲染到View

9.如何画圆弧?

首先要确定圆才能确定圆弧,圆孤它就圆上的一个角度嘛

Center:圆心

radius:圆的半径

startAngle:起始角度

endAngle:终点角度

clockwise:Yes顺时针,No逆时针

注意:startAngle角度的位置是从圆的最右侧为0度.

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(125, 125)

radius:100

startAngle:0

endAngle:M_PI * 2

clockwise:YES];

10.如果画扇形.

画扇形的方法为:先画一个圆孤再添加一个一根线到圆心,然后关闭路径.

关闭路径就会自动从路径的终点到路径的起点封闭起下

用填充的话,它会默认做一个封闭路径,从路径的终点到起点.

[path fill];

iOS开发之Quartz2D 二:绘制直线,曲线,圆弧,矩形,椭圆,圆的更多相关文章

  1. iOS开发之Quartz2D 六 绘制UIImageView

    #import <UIKit/UIKit.h> @interface XMGImageView : UIView /** <#注释#> */ @property (nonato ...

  2. iOS开发之Quartz2D详解

    1. 什么是Quartz2D? Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片( ...

  3. iOS开发之Quartz2D

    1.         Quartz2D概述及作用 Quartz2D的API是纯C语言的,Quartz2D的API来自于Core Graphics框架. 数据类型和函数基本都以CG作为前缀,比如: CG ...

  4. ios开发之Quartz2D 四:画饼图

    #import "PieView.h" @implementation PieView - (void)drawRect:(CGRect)rect { // Drawing cod ...

  5. iOS开发之Quartz2D 五:UIKIT 绘图演练,画文字,画图片

    #import "DrawView.h" @implementation DrawView -(void)awakeFromNib { // //画图片 // UIImage *i ...

  6. iOS开发之Xcode常用调试技巧总结

    转载自:iOS开发之Xcode常用调试技巧总结 最近在面试,面试过程中问到了一些Xcode常用的调试技巧问题.平常开发过程中用的还挺顺手的,但你要突然让我说,确实一脸懵逼.Debug的技巧很多,比如最 ...

  7. iOS开发之Socket通信实战--Request请求数据包编码模块

    实际上在iOS很多应用开发中,大部分用的网络通信都是http/https协议,除非有特殊的需求会用到Socket网络协议进行网络数 据传输,这时候在iOS客户端就需要很好的第三方CocoaAsyncS ...

  8. iOS开发之loadView、viewDidLoad及viewDidUnload的关系

    iOS开发之loadView.viewDidLoad及viewDidUnload的关系 iOS开发之loadView.viewDidLoad及viewDidUnload的关系    标题中所说的3个方 ...

  9. iOS 开发之Block

    iOS 开发之Block 一:什么是Block.Block的作用 UI开发和网络常见功能的实现回调,按钮事件的处理方法是回调方法. 1.     按钮事件 target action 机制. 它是将一 ...

随机推荐

  1. matlab (.m)文件生成 windows 可执行(.exe)文件

    mex -setup:设置 C 语言编译器:(如果本地安装有 visual studio 20xx 集成开发环境,则会自动选择其下的 C/C++ 编译器 ) 将运行时环境(runtime enviro ...

  2. HDU4630-No Pain No Game(离线,线段树)

    Problem Description Life is a game,and you lose it,so you suicide. But you can not kill yourself bef ...

  3. Maven搭建hadoop环境报Missing artifact jdk.tools:jdk.tools:jar:1.7

    今天,更新了工程,报错了. 项目中用了HBase,也有Hadoop相关的jar配置. pom文件, Missing artifact jdk.tools:jdk.tools:jar:1.7 Maven ...

  4. 2018/8/15 qbxt 测试

    2018/8/15 qbxt 测试 期望得分:100:实际得分:50   不知道为什么写挂了,明明是个水题 T^T 思路:模拟 注意:如果用 char 类型存储的话,如果有'z' + 9 会爆char ...

  5. [RxJS] Learn How To Use RxJS 5.5 Beta 2

    The main changes is about how you import rxjs opreators from now on. And introduce lettable opreator ...

  6. gplaycli—— 用于从 GooglePlayStore 中下载和管理 Apk 文件的命令行工具

    gplaycli-- 用于从 GooglePlayStore 中下载和管理 Apk 文件的命令行工具 这个 GooglePlay市场 中 https://play.google.com/store/a ...

  7. AAC编解码

    AAC编码可以使用faac /** 初始化 @param sampleRate 音频采样率 @param channels 通道数 @param bitSize 音频采样精度 16 */ - (voi ...

  8. 【2017 Multi-University Training Contest - Team 10】Schedule

    [链接]http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010&cid=767 [题意] 给一些区间,每台机器在这些区间 ...

  9. Tomcat redis 配置

    http://www.cnblogs.com/interdrp/p/4868740.html http://blog.csdn.net/qq584852076/article/details/4650 ...

  10. jQuery的原理

    JQ的原理 jquery-1.xxx :专门为PC端诞生的类库,兼容所有的浏览器 jquery-2.xxx:当初是为了移动端而准备的,所以IE低版本浏览器一般不兼容,但是这个版本针对移动端的事件等操作 ...