iOS 画图讲解
5、画图
(1)画线
//绘图代码写在drawRect里,view加载完成,需要显示的时候调用
//1、获取图形上下文 2、创建路径 3、把图形放入上下文 4、渲染上下文
//drawRect的rect是绘制View的bounds
//重绘
[self setNeedsDisplay];
set = setStroke(描边) + setFill(填充)
//方法一:最原始的方法
//1、获取图形上下文,CG,CoreGraphics,有关图形的框架,开发mac也可以用
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2、创建路径
CGMutablePathRef path = CGPathCreateMutable();
//设置起始点
CGPathMoveToPoint(path, NULL, 50, 50);
//画线到某一点
CGPathAddLineToPoint(path, NULL, 200, 200);
//3、把图形放入上下文
CGContextAddPath(ctx, path);
//4、渲染上下文
CGContextStrokePath(ctx);
//绘图的第二种方法
- (void)drawLine2 {
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置起始点
CGContextMoveToPoint(ctx, 50, 50);
//画线并设置终点
CGContextAddLineToPoint(ctx, 200, 200);
//渲染上下文
CGContextStrokePath(ctx);
}
//第三种方法
- (void)drawLine3 {
//贝塞尔线,UIKit
//创建路径
UIBezierPath * path = [UIBezierPath bezierPath];
//设置起点
[path moveToPoint:CGPointMake(50, 50)];
//画线并设置终点
[path addLineToPoint:CGPointMake(200, 200)];
//绘制路径
[path stroke];
}
//属性
- (void)drawCtxState {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 50, 50);
CGContextAddLineToPoint(ctx, 100, 50);
// CGContextMoveToPoint(ctx, 80, 60);
//默认下一根线的起点就是上一根线的终点
CGContextAddLineToPoint(ctx, 100, 200);
//设置线宽度
CGContextSetLineWidth(ctx, 5);
//设置连接样式
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//设置顶角样式
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置线的颜色
[[UIColor redColor] setStroke];
CGContextStrokePath(ctx);
}
//画曲线
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 50, 50);
//画曲线 arg1 上下文 arg2,3 控制点x,y arg4,5 终点x,y
CGContextAddQuadCurveToPoint(ctx, 100, 100, 250, 50);
CGContextStrokePath(ctx);
}
(2)画图形
//画矩形
UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 200, 200)];
//画圆角矩形
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 200, 200) cornerRadius:100];
//画圆弧
//Center 圆弧中心
//radius 圆弧半径
//startAngle 起始角度
//endAngle 结束角度
//clockwise YES 顺时针 NO 逆时针
CGPoint center = CGPointMake(125, 125);
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:NO];
[path addLineToPoint:center];
//封闭路径
[path closePath];
[path stroke];
//要使用setFill,路径一定是封闭的
[[UIColor greenColor] setFill];
[path fill];
//画椭圆
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 50, 200, 100)];
[path stroke];
}
6、绘制图片和文字
(1)绘制图片
- (void)drawPicture {
//图片裁剪,超出部分全部剪掉
UIRectClip(CGRectMake(0, 0, 50, 50));
UIImage * image = [UIImage imageNamed:@"01"];
//根据rect拉伸图片
[image drawInRect:CGRectMake(0, 0, 100, 100)];
[image drawInRect:rect];
//显示原图片尺寸
[image drawAtPoint:CGPointZero];
//平铺绘图
[image drawAsPatternInRect:rect];
}
(2)绘制文字
- (void)drawRect:(CGRect)rect {
// Drawing code
NSString * string = @"hgfdagskjhdcadkhdkjlashkdklhahgfdagskjhdcad";
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor yellowColor];
shadow.shadowOffset = CGSizeMake(5, 5);
//模糊度
shadow.shadowBlurRadius = 3;
NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:30],
NSForegroundColorAttributeName:[UIColor redColor],
NSStrokeColorAttributeName:[UIColor blueColor],
NSStrokeWidthAttributeName:@(2),
NSShadowAttributeName: shadow};
//不会换行
[string drawAtPoint:CGPointZero withAttributes:nil];
[string drawInRect:rect withAttributes:dict];
}
5、画图
(1)画线
//绘图代码写在drawRect里,view加载完成,需要显示的时候调用
//1、获取图形上下文 2、创建路径 3、把图形放入上下文 4、渲染上下文
//drawRect的rect是绘制View的bounds
//重绘
[self setNeedsDisplay];
set = setStroke(描边) + setFill(填充)
//方法一:最原始的方法
//1、获取图形上下文,CG,CoreGraphics,有关图形的框架,开发mac也可以用
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2、创建路径
CGMutablePathRef path = CGPathCreateMutable();
//设置起始点
CGPathMoveToPoint(path, NULL, 50, 50);
//画线到某一点
CGPathAddLineToPoint(path, NULL, 200, 200);
//3、把图形放入上下文
CGContextAddPath(ctx, path);
//4、渲染上下文
CGContextStrokePath(ctx);
//绘图的第二种方法
- (void)drawLine2 {
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置起始点
CGContextMoveToPoint(ctx, 50, 50);
//画线并设置终点
CGContextAddLineToPoint(ctx, 200, 200);
//渲染上下文
CGContextStrokePath(ctx);
}
//第三种方法
- (void)drawLine3 {
//贝塞尔线,UIKit
//创建路径
UIBezierPath * path = [UIBezierPath bezierPath];
//设置起点
[path moveToPoint:CGPointMake(50, 50)];
//画线并设置终点
[path addLineToPoint:CGPointMake(200, 200)];
//绘制路径
[path stroke];
}
//属性
- (void)drawCtxState {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 50, 50);
CGContextAddLineToPoint(ctx, 100, 50);
// CGContextMoveToPoint(ctx, 80, 60);
//默认下一根线的起点就是上一根线的终点
CGContextAddLineToPoint(ctx, 100, 200);
//设置线宽度
CGContextSetLineWidth(ctx, 5);
//设置连接样式
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//设置顶角样式
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置线的颜色
[[UIColor redColor] setStroke];
CGContextStrokePath(ctx);
}
//画曲线
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 50, 50);
//画曲线 arg1 上下文 arg2,3 控制点x,y arg4,5 终点x,y
CGContextAddQuadCurveToPoint(ctx, 100, 100, 250, 50);
CGContextStrokePath(ctx);
}
(2)画图形
//画矩形
UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 200, 200)];
//画圆角矩形
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 200, 200) cornerRadius:100];
//画圆弧
//Center 圆弧中心
//radius 圆弧半径
//startAngle 起始角度
//endAngle 结束角度
//clockwise YES 顺时针 NO 逆时针
CGPoint center = CGPointMake(125, 125);
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:100 startAngle:0 endAngle:M_PI_2 clockwise:NO];
[path addLineToPoint:center];
//封闭路径
[path closePath];
[path stroke];
//要使用setFill,路径一定是封闭的
[[UIColor greenColor] setFill];
[path fill];
//画椭圆
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 50, 200, 100)];
[path stroke];
}
6、绘制图片和文字
(1)绘制图片
- (void)drawPicture {
//图片裁剪,超出部分全部剪掉
UIRectClip(CGRectMake(0, 0, 50, 50));
UIImage * image = [UIImage imageNamed:@"01"];
//根据rect拉伸图片
[image drawInRect:CGRectMake(0, 0, 100, 100)];
[image drawInRect:rect];
//显示原图片尺寸
[image drawAtPoint:CGPointZero];
//平铺绘图
[image drawAsPatternInRect:rect];
}
(2)绘制文字
- (void)drawRect:(CGRect)rect {
// Drawing code
NSString * string = @"hgfdagskjhdcadkhdkjlashkdklhahgfdagskjhdcad";
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor yellowColor];
shadow.shadowOffset = CGSizeMake(5, 5);
//模糊度
shadow.shadowBlurRadius = 3;
NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:30],
NSForegroundColorAttributeName:[UIColor redColor],
NSStrokeColorAttributeName:[UIColor blueColor],
NSStrokeWidthAttributeName:@(2),
NSShadowAttributeName: shadow};
//不会换行
[string drawAtPoint:CGPointZero withAttributes:nil];
[string drawInRect:rect withAttributes:dict];
}
iOS 画图讲解的更多相关文章
- iOS 画图讲解2
1.图片水印 //layer上下文只能显示在drawRect里 //当开启上下文时,绘制图形即可在viewDidLoad中实现 //位图的上下文 //UIGraphicsBeginImageConte ...
- IOS NSUserDefaults 讲解 用法
IOS NSUserDefaults 讲解 用法 NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults ...
- iOS开发讲解SDWebImage,你真的会用吗?
SDWebImage作为目前最受欢迎的图片下载第三方框架,使用率很高.但是你真的会用吗?本文接下来将通过例子分析如何合理使用SDWebImage. 使用场景:自定义的UITableViewCell上有 ...
- 利用IOS画图功能画出五角星,并且可以调整五角星的填充范围
我们要花的为一个黄色的五角星并且其中的填充黄色能够任意调整,比如只填满半个五角星,或者只填满一个角等等. 首先要重写DrawRect 方法,然后在这里实现我们的画图代码. - (void)drawRe ...
- iOS 画图基础
基础要点: 1,画图不可以在 ViewController 里,而是应该在一个 UIView 的子类中,比如新建一个 DrawView 继承自 UIView. 2,覆盖 UIView 的 drawRe ...
- ios 画图总结
0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文1 CGContextMoveToPoint 开始画线2 CGContextA ...
- ios 深入讲解iOS键盘一:控制键盘隐藏显示
在iOS的开发中,我们一般使用UITextField.UITextView处理文字输入等操作,大部分情况下我们只需要一两行代码去手动管理键盘的显示隐藏:让UITextField或UITextView成 ...
- ios开发讲解之anchorPoint和position详解
引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与posi ...
- iOS 谓词讲解
1.NSPredicate (1)比较运算符 1.比较运算符 > .< .== . >= .<= . != 运算符还可以跟逻辑运算符一起使用,&& , || ...
随机推荐
- VPN 隧道协议PPTP、L2TP、IPSec和SSLVPN的区别
最近软矿频繁地介绍了各种VPN,有免费的PacketiX.NET和Hotspot Shield,有付费的Astrill VPN,iVPN和PureVPN.在介绍这些VPN的时候,常常会说到PPTP.L ...
- HDU2897邂逅明下(博弈)
题目是说每次每个人可以取[p,q],而且是最后一个不得不取完的人输 这道题刚刚看别人过,还一直纠结感觉不会做,然后想到1+q的倍数,还是不会,想到p+q的倍数,却发现最后一个取的人是输的,然后就更加无 ...
- Lotus 迁移到Exchange 2010 之准备使用Transport 同步Lotus 相关信息!
这里我们先来分析下Lotus迁移到Exchange2010 的一些原理,由于存在一定周期的共存时间,因此在共存期间必须来实现相应的同步计划,整个同步计划包含了如下的同步计划:
- 【绝密外泄】风哥Oracle数据库DBA高级工程师培训视频教程与内部资料v0.1
[绝密外泄]风哥Oracle数据库DBA高级工程师培训视频教程与内部资料v0.1 由于是[绝密外泄]资料,防止被查,需要的小伙伴赶紧下载附件中的课件文档.由于视频太大了,已放在百度网盘了,已经在附中说 ...
- 有关获取session属性时报nullPointException(空指针异常)的解决方案
一般我们在从session中获取数据时,需要先进行赋值,也就是必须先进行session.setAttribute(String,Object)方法进行赋值,然后我们才能从session中获取内容,但是 ...
- 详解MyEclipse10 安装Spket 1.6.23(支持Extjs4.1.1及jQuery1.8)
用MyEclipse10安装Spket主要有3种方式:在线下载更新.下载Zip覆盖.下载jar包安装.我用在线安装尝试了N次终于还是失败,只好下载jar包来安装,在失败了M次之后终于安装成功,现在网上 ...
- 切取图片的一部分(利用CCRenderTexture)
转自:http://blog.csdn.net/wcjwdq/article/details/37932769 显示图片时,在项目中经常会用只读取图片的一部分,而不是全部. 错误方式:很多人这时候会采 ...
- Trie树学习2
数组实现的Trie树 字符容量有限,能够使用链表实现更为大容量的Trie #include <iostream> #include <cstdio> #include < ...
- 使用GLSL实现的海洋效果 【转】
http://bbs.osgchina.org/viewthread.php?tid=342&extra=page%3D3 虽说自己原创的部分并不算多,不过总算是调试通过了,中间有多次严重的死 ...
- 使用ServiceStackRedis链接Redis简介 [转]
注:关于如何在windows,linux下配置redis,详见这篇文章:) Discuz!NT中的Redis架构设计 目前网上有一些链接Redis的C#客户端工具,这里介绍其中也 ...