iOS小明开发笔记(十二) (Quartz2D简单使用一)
来源:互联网 发布:数据库模型图是什么 编辑:程序博客网 时间:2024/06/06 14:10
#import "MMLineView.h"
@implementation MMLineView
// 当自定义view第一次显示出来的时候就会调用drawRect方法
- (void)drawRect:(CGRect)rect
{
// 1.取得和当前视图相关联的图形上下文(因为图形上下文决定绘制的输出目标)/
// 如果是在drawRect方法中调用UIGraphicsGetCurrentContext方法获取出来的就是Layer的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();//不需要*,同id
// 2.绘图(绘制直线),保存绘图信息
// 设置起点
CGContextMoveToPoint(ctx, 20, 100);
//设置终点
CGContextAddLineToPoint(ctx, 300, 100);
//设置绘图的状态
//设置线条的颜色为蓝色
CGContextSetRGBStrokeColor(ctx, 0, 1.0, 0, 1.0);
//设置线条的宽度
CGContextSetLineWidth(ctx, 15);
//设置线条起点和终点的样式为圆角
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置线条的转角的样式为圆角
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//3.渲染(绘制出一条空心的线)
CGContextStrokePath(ctx);
// //注意线条不能渲染为实心的
// CGContextFillPath(ctx);
//设置第二条线
//设置第二条线的起点
CGContextMoveToPoint(ctx, 50, 200);
//设置第二天线的终点(自动把上一条直线的终点当做起点)
CGContextAddLineToPoint(ctx, 300, 60);
//设置绘图的状态
// CGContextSetRGBStrokeColor(ctx, 1.0, 0.7, 0.3, 1.0);
//第二种设置颜色的方式
[[UIColor grayColor] set];
//设置线条的宽度
CGContextSetLineWidth(ctx, 10);
//设置线条的起点和终点的样式
CGContextSetLineCap(ctx, kCGLineCapButt);
//渲染第二条线的图形到view上
//绘制一条空心的线
CGContextStrokePath(ctx);
}
@end
二、画三角形
#import "MMRectView.h"
@implementation MMRectView
- (void)drawRect:(CGRect)rect
{
//1.获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.绘制三角形
//设置起点
CGContextMoveToPoint(ctx, 20, 100);
//设置第二个点
CGContextAddLineToPoint(ctx, 40, 300);
//设置第三个点
CGContextAddLineToPoint(ctx, 200, 200);
//设置终点
// CGContextAddLineToPoint(ctx, 20, 100);
//关闭起点和终点
CGContextClosePath(ctx);
// 3.渲染图形到layer上
CGContextStrokePath(ctx);
}
@end
三、画四边形
#import "MMRect.h"
@implementation MMRect
- (void)drawRect:(CGRect)rect
{
//1.获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.画四边形
CGContextAddRect(ctx, CGRectMake(20, 20, 150, 100));
// 如果要设置绘图的状态必须在渲染之前
// CGContextSetRGBStrokeColor(ctx, 1.0, 0, 0, 1.0);
// 绘制什么类型的图形(空心或者实心).就要通过什么类型的方法设置状态
// CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);
// 调用OC的方法设置绘图的颜色
// [[UIColor purpleColor] setFill];
// [[UIColor blueColor] setStroke];
// 调用OC的方法设置绘图颜色(同时设置了实心和空心)
// [[UIColor greenColor] set];
[[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] set];
//3.渲染图形到layer上
//空心的
CGContextStrokePath(ctx);
//实心的
// CGContextFillPath(ctx);
}
@end
四、画圆
(1)
- (void)drawRect:(CGRect)rect
{
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 画圆
CGContextAddArc(ctx, 100, 100, 50, 0,2 * M_PI, 0);
// 3.渲染 (注意,画线只能通过空心来画)
// CGContextFillPath(ctx);
CGContextStrokePath(ctx);
}
(2)
// 画圆
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.画圆
CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 50, 50));
[[UIColor greenColor] set];
// 3.渲染
// CGContextStrokePath(ctx);
CGContextFillPath(ctx)
(3)
// 画椭圆
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.画圆
CGContextAddEllipseInRect(ctx, CGRectMake(50, 100, 100, 230));
[[UIColor purpleColor] set];
// 3.渲染
// CGContextStrokePath(ctx);
CGContextFillPath(ctx);
五、画圆弧
(1)
// 画圆弧
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.画圆弧
// x/y 圆心
// radius 半径
// startAngle 开始的弧度
// endAngle 结束的弧度
// clockwise 画圆弧的方向 (0顺时针, 1 逆时针)
// CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI_2, 0);
CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
CGContextClosePath(ctx);
// 3.渲染
// CGContextStrokePath(ctx);
CGContextFillPath(ctx);
(2)
// 1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.画饼状图
// 画线
CGContextMoveToPoint(ctx, 100, 100);
CGContextAddLineToPoint(ctx, 100, 150);
// 画圆弧
CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);
// CGContextAddArc(ctx, 100, 100, 50, -M_PI, M_PI_2, 1);
// 关闭路径
CGContextClosePath(ctx);
[[UIColor brownColor] set];
// 3.渲染 (注意,画线只能通过空心来画)
CGContextFillPath(ctx);
//CGContextStrokePath(ctx);
- iOS小明开发笔记(十二) (Quartz2D简单使用一)
- iOS小明开发笔记(十三) (Quartz2D简单使用二)
- iOS陆哥开发笔记(十五) (Quartz2D简单使用一)
- iOS小明开发笔记(十一) (Quartz2D简单介绍)
- iOS开发—Quartz2D简单使用(一)
- iOS开发UI篇—Quartz2D简单使用(一)
- iOS开发UI篇—Quartz2D简单使用(一)
- iOS开发UI篇—Quartz2D简单使用(一)
- iOS陆哥开发笔记(十六) (Quartz2D简单使用二)
- iOS小明开发笔记(十四) (Quartz2D图形上下文栈)
- iOS小明开发笔记(十五) (Quartz2D矩阵操作)
- iOS小明开发笔记(十六) (Quartz2D图片剪切)
- iOS小明开发笔记(十七) (Quartz2D屏幕截图)
- iOS开发笔记>> Quartz2D简单介绍
- 【iOS开发】Quartz2D的简单使用
- 【iOS开发】Quartz2D的简单使用
- iOS开发UI高级—19Quartz2D简单使用(一)
- iOS陆哥开发笔记(十四) (Quartz2D简单介绍)
- JAVA操作Excel文件
- swift-集合类型01-数组的基本操作
- spring是怎么知道哪个方法或者类上有@requestMapping这个注解
- SMC计算节点重刷系统步骤
- hashMap源码分析
- iOS小明开发笔记(十二) (Quartz2D简单使用一)
- 经典队列存取
- 文字自适应label的frame
- 未备份etc下ssh目录之设置节点间无密码访问步骤
- Dsoframer注册方法
- 查找单链表倒数第k个元素
- 搭建javaweb项目
- Swift中文教程(十三) 继承
- Gerrit 使用