Quartz2D画图
来源:互联网 发布:上海浦东机场有没有mac 编辑:程序博客网 时间:2024/05/18 01:16
- (void)drawRect:(CGRect)rect;方法中画图
实例如下:
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
//用点和线画三角形和正方形
// drawRectAndTriangle();
//画矩形
// draw4Rect();
//画原园
// drawCircle();
//画弧线
drawARC();
}
/**
* 把图片画到view上去
*/
void drawImage()
{
UIImage *img = [UIImage imageNamed:@"xcode"];
//在矩形中画image,图片根据矩形的宽高而拉伸
[img drawInRect:CGRectMake(10, 10, 230, 230)];
UIImage *img2 = [UIImage imageNamed:@"xcode2"];
//根据图片的大小平铺整个Rect
[img2 drawAsPatternInRect:CGRectMake(10, 10, 220, 220)];
}
/*
*画文字
*/
void drawStr()
{
//直接把文字画在view上,不需要上下文
NSString *str = @"哈哈";
NSMutableDictionary *strdict = [NSMutableDictionary dictionary];
strdict[NSFontAttributeName] = [UIFont systemFontOfSize:17];//字体大小
strdict[NSForegroundColorAttributeName] = [UIColor greenColor];//字体颜色
// [str drawAtPoint:CGPointMake(10, 10) withAttributes:strdict];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect r = CGRectMake(50, 50, 100, 100);
CGContextAddRect(ctx, r);
CGContextFillPath(ctx);
//把文字写入到矩形里
[str drawInRect:r withAttributes:strdict];
}
/**
* 画弧线
*/
void drawARC()
{
//获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(ctx, 0.4, 0.4, 0.2, 1);
CGContextSetLineWidth(ctx, 3);
//x/y:圆心坐标
//radius:半径
//startAngle:开始弧度
//endAge:结尾弧度
//clockwise:顺逆失真(0:顺时针 1:逆时针)
CGContextAddArc(ctx, 70, 100, 30, 0, M_PI, 1);
//渲染
CGContextStrokePath(ctx);
CGContextAddArc(ctx, 150, 100, 30, 0, M_PI, 1);
CGContextStrokePath(ctx);
CGContextAddArc(ctx, 110, 130, 50, 0, M_PI, 0);
CGContextStrokePath(ctx);
//画1/4圆
CGContextMoveToPoint(ctx, 250, 130);
CGContextAddLineToPoint(ctx, 250, 200);
//画圆弧线(顺时针从二分之π到π)
CGContextAddArc(ctx, 250, 130, 70, M_PI_2, M_PI, 0);
//闭线
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
}
/**
* 画原型
*/
void drawCircle()
{
//获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置线条宽度
CGContextSetLineWidth(ctx, 5);
//设置线条颜色
CGContextSetRGBStrokeColor(ctx, 0.4, 0.5, 0.1, 1);
//画一个坐标为50,10,宽50,高40的椭圆
CGContextAddEllipseInRect(ctx, CGRectMake(50, 90, 50, 40));
CGContextAddEllipseInRect(ctx, CGRectMake(90, 90, 50, 40));
CGContextAddEllipseInRect(ctx, CGRectMake(130, 90, 50, 40));
CGContextAddEllipseInRect(ctx, CGRectMake(170, 90, 50, 40));
CGContextAddEllipseInRect(ctx, CGRectMake(210, 90, 50, 40));
CGContextStrokePath(ctx);
}
/**
* 画矩形
*/
void draw4Rect()
{
//获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// CGContextSetRGBFillColor(ctx, 0.3, 0.4, 0.3, 0.8);//设置填充颜色
//set:设置实心和空心的颜色
[[UIColor greenColor]set];
//setFill:设置实心的颜色
[[UIColor greenColor] setFill];
//setStroke:设置实心的颜色
[[UIColor greenColor] setStroke];
//画一个100x200的矩形
CGContextAddRect(ctx, CGRectMake(10, 10, 100, 200));
//渲染(实心图形)
CGContextFillPath(ctx);
}
/**
* 用点和线画三角形和正方形
*/
void drawRectAndTriangle()
{
//获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置头尾部的样式
CGContextSetLineCap(ctx, kCGLineCapRound);//设置线头尾部都为原型
//设置转折点样式
CGContextSetLineJoin(ctx, kCGLineJoinRound);//设置线的转折点为原型
//设置线的宽度
CGContextSetLineWidth(ctx, 10);
//画正方形
//设置线的颜色
CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);//红色
//画一个点
CGContextMoveToPoint(ctx, 30,30);
//画一条线
CGContextAddLineToPoint(ctx, 30, 120);
CGContextAddLineToPoint(ctx, 120, 120);
CGContextAddLineToPoint(ctx, 120, 30);
CGContextAddLineToPoint(ctx, 30, 30);
//渲染显示到view上面(实心图形)
CGContextStrokePath(ctx);
/**
* 画三角形
*/
//设置线的颜色
CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);//绿色
//画一个点
CGContextMoveToPoint(ctx, 130,130);
//画一条线
CGContextAddLineToPoint(ctx, 130, 200);
CGContextAddLineToPoint(ctx, 200, 130);
// CGContextAddLineToPoint(ctx, 130, 130);
CGContextClosePath(ctx);//封闭
//渲染显示到view上面(空心图形)
CGContextStrokePath(ctx);
}
- Quartz2D画图
- Quartz2d 画图
- Quartz2D 画图学习
- iOS-Quartz2D画图
- Quartz2D 画图(圆形头像)
- iOS-Quartz2D 之 画图预览笔记
- Quartz2D 画图工具的基本使用
- 浅谈 Quartz2D 在开发中的用处 - 画图形
- quartz2d
- Quartz2d
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- Quartz2D
- 输出每月的天数--华为机试题
- 【网络流模板】
- java打乱ArrayList生成一个随机ArrayList列表
- PowerDesigner生成word文档
- 微信公众平台开发教程(三) 基础框架搭建
- Quartz2D画图
- 不需要任何参数
- Android中如何自己定义吐司(Toast)
- 二叉树先序,中序,后序遍历非递归实现
- Deep Learning 读书笔记(四):关于卷积神经网络的各种
- 微信公众平台开发教程(四) 实例入门:机器人(附源码)
- 即将到来的教训
- Linux系统信号
- OC类属性