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 ...
随机推荐
- 网站后台调用winform MessageLoopApartment
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- linux后台运行和关闭、查看后台任务(转)
转自:http://www.cnblogs.com/kaituorensheng/p/3980334.html fg.bg.jobs.&.nohup.ctrl+z.ctrl+c 命令 一.&a ...
- JAVA ,Map接口 ,迭代器Iterator
1. Map 接口概述 java.util.Map 接口描述了映射结构, Map 接口允许以键集.值集合或键 - 值映射关系集的形式查看某个映射的内容. Java 自带了各种 Map 类. 这些 ...
- 如何取消win10电脑自动更新
windows 10系统中关闭windows自动更新步骤如下:1.按键盘上的“Windows徽标键+R”组合键,可以调出“运行”窗口. 2.输入gpedit.msc,单击“确定”,可以打开“本地组策略 ...
- 简单的线性分类——MATLAB,python3实现
看李政轩老师讲的Kernel,讲的非常好!前面有几道作业题,用MATLAB简单做了下,不知道对不对,错误之处还请指出. 题目是这样的. 一.MATLAB版本: clear; clc % 生成train ...
- Mybatis-动态 SQL
MyBatis 的强大特性之一便是它的动态 SQL. 如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格,还要注意省 ...
- 关于在win7内集成usb3.0驱动。
mac air 装了win7但是折腾良久还是无法升级,只能是重新安装. 很蛋疼.bootcamp 老是找不到驱动.只能是手动分区后U盘引导安装. 驱动的下载,直接在Os x 下用bootcamp 下载 ...
- oracle 存储过程的写法
create or replace procedure Getyc is v_id VARCHAR2(36); v_date VARCHAR2(4); begin declare begi ...
- android之ViewPager
在android中ViewPager是非常常用的控件.它在android.support.v4.view.ViewPager下.你们自己可以进http://developer.android.com/ ...
- Android——配置文件的保存SharedPreferences进行数据存储
很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友.对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果 ...