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 ...
随机推荐
- 在VS下使用 GitFlow管理项目开发
在VS下使用 GitFlow管理项目开发 1.右键将你的解决方案添加到源代码管理,如果你的VS没有安装git,会提示安装,安装完成之后,在团队资源管理可以看到如下界面 (图一) 2.安装gitflow ...
- (转)PhoneGap开发环境搭建
(原)http://www.cnblogs.com/Random/archive/2011/12/28/2305398.html PhoneGap开发环境搭建 项目中要用PhoneGap开发,了解 ...
- UE4 Tutorial - Custom Mesh Component 用于绘制自定义网格的插件CustomMeshComponent
UE4 中用于绘制自定义网格的插件CustomMeshComponent. 转载: UE4 Tutorial - Custom Mesh Component Over the last few w ...
- [转]Centos 6.5 安装 Scrapy 0.22.2成功
0. python -V (此时显示为2.6.6) 1. yum -y update 2. yum groupinstall -y development 3. yum ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- 初试微信小程序
2016年11月3日,微信小程序终于公测了,大家可以正式开发了.早在这之前,应公司要求,和同事就早早的试了一下微信小程序的开发,特此记录一下: 微信官方小程序文档:https://mp.weixin. ...
- Jena Fuseki 102
Version Fuseki v1 Fuseki v2 since Jena 2.13.0 Both v1 and v2 are active and maintained.[2015/06/29] ...
- JSBinding / Code Snippets
new a gameobject & overloaded methds var go1 = new UnityEngine.GameObject.ctor(); var go2 = new ...
- AS2使用ExternalInterface
以下代码是帧脚本(选中某帧F9,粘贴) import flash.external.ExternalInterface; // 假的,目的是为了执行createButton里面的ExternalInt ...
- C#操作Word的辅助类(word2003) 修改完善版
转自:http://blog.csdn.net/jiutao_tang/article/details/6567608 该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插 ...