CGContextRef 绘图
如何绘制一个矩形
添加一个属性
@property(nonatomic,strong) NSString* RectNumber;
1. 首先重写UIview的drawRect方法
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
int number = [self.RectNumber intValue];
int x = 50;
int y = 0;
CGContextBeginPath(ctx);
//设置图形间距
for (int i=0; i<number; i++) {
if (i%3 == 0) {
y+=100;
x=50;
}
[self AddRect:ctx leftPoint:x rightPoint:y width:90 height:50];
x+=100;
}
CGContextSetRGBStrokeColor(ctx, 0.5, 0, 0, 1);
CGContextSetRGBFillColor(ctx, 0, 1, 0, 1);
CGContextDrawPath(ctx, kCGPathEOFillStroke);
//打开路径
CGContextStrokePath(ctx);
//关闭路径
CGContextClosePath(ctx);
}
2. 实现自定义方法
-(void)AddRect:(CGContextRef)r leftPoint:(CGFloat)x1 rightPoint:(CGFloat)x2 width:(CGFloat)w height:(CGFloat)h
{
CGContextMoveToPoint(r, x1, x2);
//添加一条连接到右上角的线
CGContextAddLineToPoint(r, x1+w, x2);
//添加一条连接到右下角的先
CGContextAddLineToPoint(r, x1+w, x2+h);
//添加一条连接到左下角的线
CGContextAddLineToPoint(r, x1, x2+h);
//添加一条连接到左上角的线
CGContextAddLineToPoint(r, x1, x2);
}
3添加一个生成按钮
field2 = [[UITextField alloc]initWithFrame:CGRectMake(50, 150, 200, 40)];
field2.placeholder = @"输入要多少个矩形";
[self.view addSubview:field2];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(100, 200, 80, 40);
[btn2 setTitle:@"生成" forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn2 addTarget:self action:@selector(click3) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
self.view.backgroundColor = [UIColor whiteColor];
4 viewController 里实现生产方法
curRect = [[CurRectView alloc]initWithFrame:self.view.frame];
curRect.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click4)];
[curRect addGestureRecognizer:tap];
curRect.RectNumber = field2.text;
[self.view addSubview:curRect];
5 手势移除视图
[curRect removeFromSuperview];
如何绘制五角星
添加一个属性
@property (nonatomic,strong) NSString *starNumber;
1. 首先重写UIview的drawRect方法
-(void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
int number = [self.starNumber intValue];
int x = 50;
int y = 0;
CGContextBeginPath(ctx);
//设置图像间距
for (int i=0; i<number; i++) {
if (i%4 == 0) {
y+=100;
x=50;
}
[self CGContextAddStar:ctx starCount:5 starX:x starY:y starSize:50];
x+=100;
}
//图形边框颜色
CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
//设置填充颜色
CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);
CGContextDrawPath(ctx, kCGPathEOFillStroke);
//打开路径
CGContextStrokePath(ctx);
//关闭路径
CGContextClosePath(ctx);
}
2. 实现自定义方法
-(void)CGContextAddStar:(CGContextRef)c starCount:(NSInteger)n starX:(CGFloat)dx starY:(CGFloat)yx starSize:(NSInteger)size
{
//m_pi 是圆周率
CGFloat dig = 4*M_PI/n;
//移动到制定点
CGContextMoveToPoint(c, dx, yx+size);
for (int i = 1; i<=n; i++) {
CGFloat x = sin(i*dig);
CGFloat y = cos(i*dig);
//绘制从当前点到制定点的线条
CGContextAddLineToPoint(c, x*size+dx, y*size+yx);
}
}
3添加一个生成按钮
field1 = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 200, 40)];
field1.placeholder = @"输入要多少个五角星";
[self.view addSubview:field1];
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(100, 100, 80, 40);
[btn1 setTitle:@"生成" forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(click1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
self.view.backgroundColor = [UIColor whiteColor];
4 viewController 里实现生产方法
cur = [[CurView alloc]initWithFrame:self.view.frame];
cur.backgroundColor = [UIColor whiteColor];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click2)];
[cur addGestureRecognizer:tap];
cur.starNumber = field1.text;
[self.view addSubview:cur];
5 手势移除视图
[cur removeFromSuperview];
注 :viewcontroller 里添加全局变量
UITextField *field1,*field2;
UIButton *btn1,*btn2;
CurView *cur;
CurRectView *curRect;
CGContextRef 绘图的更多相关文章
- iOS_Quartz 2D绘图
目 录: 一.基础知识掌握 二.Quartz 2D绘图基础:CGContextRef实现简单地绘制图形 三.CGContextRef实现文字.图片.基于路径的图形绘制 四.在内存中绘制位图 五.添加 ...
- iOS绘图CGContextRef详解
转自:http://blog.csdn.net/u014286994/article/details/51333118 /* CoreGraphics - CGContext.h */ /** Gra ...
- iOS CGContextRef/UIBezierPath(绘图)
绘图的底层实现方法 注意:在drawRect方法中系统会默认创建一个上下文(C语言类型)在其他方法中不会有这样一个上下文(可以自己测试) @implementation DrawView //注意,在 ...
- iOS 2D绘图 (Quartz2D)之Transform(CTM,Translate,Rotate,scale)
前言:Quartz默认采用设备无关的user space来进行绘图,当context(画板)建立之后,默认的坐标系原点以及方向也就确认了,可以通过CTM(current transformation ...
- iOS 2D绘图 (Quartz2D)之路径(stroke,fill,clip,subpath,blend)
像往常一样 这个系列的博客是跟着大神的脚步来的.按照往例 在此贴出原博客的出处: http://blog.csdn.net/hello_hwc?viewmode=list我对大神的崇拜之情 如滔滔江水 ...
- iOS 2D绘图 (Quartz 2D) 概述
本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=list 由于自己的项目需要,从网络上下载了许多关于绘制图形的demo,只是用在自己的项目中,很多地方 ...
- 1.1 Quartz 2D 绘图
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” Quartz2D 绘图主要步骤: 1. 获取[图形上下文]对象 —— (拿到草稿纸 ...
- IOS 绘图教程Quartz2D
http://www.cocoachina.com/industry/20140115/7703.html http://www.cnblogs.com/wendingding/p/3803020.h ...
- iOS CGContextRef画图时的常用方法
UIView的drawRect方法 CoreGraphics绘图 综述:描述系统会调用UIView的drawRect方法,所以coreGraphics的所有实现代码放在该函数内,setNeedsDis ...
随机推荐
- oracle查看当前用户权限
查看用户和默认表空间的关系select username,default_tablespace from dba_users;--查看当前用户能访问的表select * from user_table ...
- 2-legged oauth & 3-legged oauth
3-legged oauth resource owner, client, server. resource owner 给client访问权限去访问resource owner在server上的r ...
- 【转载】【Windows批处理IV】批量进行文件重命名
1.过滤文件名中所有数字.汉字.特殊字符(含空格) @echo off for %%a in (*.*) do ( if "%%~nxa" neq "%~nx0" ...
- Oracle数据库3
在前两章,我们学习了SQL语言中基本的一些查询语句,也就是数据库查询语言DQL,今天我们要介绍的数据库操作语言DML 数据库中,我们除了查询之外,最主要的就是日常的增.删.改.查了. 数据库操作语言 ...
- url中的百分号转译
有一次发现自己输入的url中含有中文的时候,他会转化为%XXXX的格式. 于是想怎么把他给转换回去,于是使用了urllib库 #-*-coding:utf8 -*- import urllib url ...
- A - Humble Numbers
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- PAT (Basic Level) Practise:1031. 查验身份证
[题目链接] 一个合法的身份证号码由17位地区.日期编号和顺序编号加1位校验码组成.校验码的计算规则如下: 首先对前17位数字加权求和,权重分配为:{7,9,10,5,8,4,2,1,6,3,7,9, ...
- 通过printf设置Linux终端输出的颜色和显示方式
转载自:http://www.cnblogs.com/clover-toeic/p/4031618.html 在Linux终端下调试程序时,有时需要输出大量信息.若能控制字体的颜色和显示方式,可使输出 ...
- Asp:Button控件onclick事件无刷新页面提示消息
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptMana ...
- yii去除index.php的入口脚本显示为seo友好的url
1.去除入口脚本需要在重写url,如果你的webserver软件时Apache的话,必须配置httpd.conf,搜索“LoadModule rewrite_module modules/mod_re ...