iOS小画板画线总结
一:基本画线:
使用贝赛尔曲线画:
//创建路径
UIBezierPath* aPath = [UIBezierPath bezierPath];
//设置线宽
aPath.lineWidth = 5.0;
//线条拐角
aPath.lineCapStyle = kCGLineCapRound;
//终点处理
aPath.lineJoinStyle = kCGLineCapRound;
//画线的起点
[aPath moveToPoint:CGPointMake(100.0, 0.0)];
//画线的终点
[aPath addLineToPoint:CGPointMake(200.0, 40.0)];
[aPath addLineToPoint:CGPointMake(160, 140)];
[aPath addLineToPoint:CGPointMake(40.0, 140)];
[aPath addLineToPoint:CGPointMake(0.0, 40.0)];
[[UIColor RedColor] set] //设置渲染的颜色
[aPath closePath]; //第五条线通过调用closePath方法得到的
[aPath stroke]; //Draws line 根据坐标点连线渲染
最后一句若为 [aPath fill ];意思是填充渲染
注:
最后渲染的代码要写在drawRect方法中
除了画直线
+ (UIBezierPath *)bezierPathWithRect:(CGRect)rect ; //画矩形
+ (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect ; //画圆或者椭圆
+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:( BOOL )clockwise; //画弧线
二、画虚线的方法:
第一种:
UIBezierPath* aPath = [BezierPath bezierPath];
aPath.lineWidth = 5.0;
[aPath moveToPoint:CGPointMake(100.0, 0.0)];
[aPath addLineToPoint:CGPointMake(200.0, 40.0)];
[[UIColor RedColor] set] //设置渲染的颜色
CGFloat dashPattern[] = {8,7};// 8实线,7空白
[aPath setLineDash:dashPattern count:1 phase:1];
[aPath stroke]; //Draws line 根据坐标点连线渲染
第二种:
- (void)drawLineWithNumArr:(NSArray *)lengthArr
{
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:self.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame))];
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
// 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:[UIColor whiteColor].CGColor];
// 设置虚线宽度
[shapeLayer setLineWidth:self.m_LineWidth];
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer setLineDashPattern:lengthArr];
// 设置路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
CGPathAddLineToPoint(path, NULL,CGRectGetWidth(self.frame), 0);
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[self.layer addSublayer:shapeLayer];
}
用法:传进来一个数组:
arr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:8], [NSNumber numberWithFloat:3],nil]; 可以传多个,分别为线的长度,空白处的长度,线的长度,空白处的长度............依此类推
arr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:8], [NSNumber numberWithFloat:3], [NSNumber numberWithFloat:5], [NSNumber numberWithFloat:3]nil];
//这样的数组依照线的长度和空白处的长度依次为8,3,5,3........8,3,5,3
第二种相对于第一种可以设置出每小段长度不同的虚线。
iOS小画板画线总结的更多相关文章
- [修复] Firemonkey 画线问题(Android & iOS 平台)
问题:官方 QC 的一个 Firemonkey 移动平台画线问题: RSP-14309: [iOS & Android] Delphi 10.1 Berlin - drawing proble ...
- ios cocos2d 画线出现闪烁问题
根据http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/ 用cocos2d ...
- HTML5自学笔记[ 12 ]canvas绘图小示例之鼠标画线
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
// Only override drawRect: if you perform custom drawing. // An empty implementation adversely affec ...
- [ios]MKMapView中使用MKPolyline画线
参考:http://blog.sina.com.cn/s/blog_9e8867eb0101dt76.html 首先在MapView.h中 #import <MapKit/MapKit.h> ...
- IOS 绘制基本图形( 画圆、画线、画圆弧、绘制三角形、绘制四边形)
// 当自定义view第一次显示出来的时候就会调用drawRect方法- (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIG ...
- IOS简单画板实现
先上效果图 设计要求 1.画笔能设置大小.颜色 2.有清屏.撤销.橡皮擦.导入照片功能 3.能将绘好的画面保存到相册 实现思路 1.画笔的实现,我们可以通过监听用户的 平移手势 中创建 UIBezie ...
- matplotlib画线(2)
这篇随笔是matplotlib画线的补充>>> #nocl参数控制图例中有几列,>>> import numpy as np>>> import ...
- canvas小画板--(1)平滑曲线
功能需求 项目需求:需要实现一个可以自由书写的小画板 简单实现 对于熟悉canvas的同学来说,这个需求很简单,短短几十行代码就能实现: <!doctype html> <html& ...
随机推荐
- dateset添加一列
ds.Tables[].Columns.Add("short_name", System.Type.GetType("System.String"));//直接 ...
- Git配置姓名和邮箱问题
今天在安装Git for windows完成后,配置姓名和邮箱.按照廖雪峰老师的步骤,在开始菜单里找到"Git"->"Git Bash",单击后并没有跳出 ...
- nginx 配置https upstream 跳转失败
访问首页没问题,但是在登录跳转重定向时域名被修改成upstream的名字 如果需要跳转的话会出现下面的情况: http://test-xxx-emove_pools/beehive/index ser ...
- 如何使用Android JetPlayer类
在Android中,还提供了对Jet播放的支持,Jet是由OHA联盟成员SONiVOX开发的一个交互音乐引擎.其包括两部分:JET播放器和JET引擎.JET常用于控制游戏的声音特效,采用MIDI(Mu ...
- 【Java EE 学习 50】【Spring学习第二天】【使用注解的DI实现】【spring中的继承】【动态代理伪hibernate实现】
一.使用注解的DI实现 1.@Resource 使用该注解能够实现引用型属性的DI实现,该注解能够根据属性名和属性类型自动给属性赋值.一般使用@Resource(name="student& ...
- 【leetcode】Max Points on a Line
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...
- [译]:Orchard入门——给网站添加页面
原文链接:Adding Pages to Your Site 注:内容为官方文档翻译,本人遇到的page中间是布局,而非官网的body--但此内容可以在内容定义里自行修改(本文不做介绍) 在创建Orc ...
- 判断横屏竖屏,然后CSS重新计算
function hengshuping(){ if(window.orientation==180||window.orientation==0){ alert("竖屏状态!") ...
- 对JavaScript中异步同步机制以及线程深入了解
今天在网上看到各种对Js异步同步单线程多线程的讨论 经过前辈们的洗礼 加上鄙人小小的理解 就来纸上谈兵一下吧~ Js本身就是单线程的 至于为什么Js是单线程的 那就要追溯到Js的历史了 总而言之 由于 ...
- 激活Windows 8.1 RTM原来如此简单
日前,Windows 8.1 RTM各种版本已经在坊间泄露开来,许多迫不及待的用户也开始跃跃欲试,但可能有人会疑惑,Windows 8.1RTM该如何激活?其实,它远比你想象的要简单. 实际上,Win ...