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 ...
随机推荐
- Allegro建立引脚封装概念名词梳理
首先感谢于博士的60讲的Cadence教学视频,老师讲的还是很有耐心,很细致,谢谢! 目前还只是看到建立PCB封装这一块,正好手头上有个案子在做,边做边学的进度还是要好很多.以前的工作对原理图这一块的 ...
- Python OpenCV —— Modifying
一些索引和修改图像像素点数据等的操作,可打印观察运行结果. # -*- coding: utf-8 -*- """ Created on Wed Sep 28 00:11 ...
- SSD固态硬盘使用注意事项
SSD固态硬盘的结构和运行方式不同于传统硬盘,在购买以后强烈建议进行一些设置以达到最优的运行效能,延长SSD的使用寿命.以下第一部分是一些可以在网上找到的关于SSD硬盘相关的注意事项,已经介绍很多,这 ...
- PHP Html5上传大文件
html前台代码: <form id="upload_form" name="upload_form" action="javascript:i ...
- Visual Studio 2012+jQuery-1.7.1
今天用Visual Studio 2012开发一个网站项目,在集成jqplot图表控件并进行调试的时候(使用的是MVC4框架),加载网页绘制图表的时候总是报错(提示$.jqplot.barRender ...
- Java数据结构和算法之哈希表
五.哈希表 一般的线性表.树中,记录在结构中的相对位置是随机的即和记录的关键字之间不存在确定的关系,在结构中查找记录时需进行一系列和关键字的比较.这一类查找方法建立在“比较”的基础上,查找的效率与比较 ...
- PAT (Basic Level) Practise:1018. 锤子剪刀布
[题目链接] 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方分别出什么手势的胜算最大. 输入格式: 输入第1行 ...
- 无需输入密码的scp/ssh/rsync操作方法
一般使用scp/ssh/rsync传输文件时,都需要输入密码.下面是免密码传输文件的方法. 假设要在两台主机之间传送文件,host_src & host_dst.host_src是文件源地址所 ...
- C++ Primer : 第十三章 : 动态内存管理类
/* StrVec.h */ #ifndef _STRVEC_H_ #define _STRVEC_H_ #include <memory> #include <string> ...
- odoo.cli.main()做了什么?
先把代码贴过来 def main(): args = sys.argv[1:] # The only shared option is '--addons-path=' needed to disco ...