一:基本画线:

使用贝赛尔曲线画:

//创建路径

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小画板画线总结的更多相关文章

  1. [修复] Firemonkey 画线问题(Android & iOS 平台)

    问题:官方 QC 的一个 Firemonkey 移动平台画线问题: RSP-14309: [iOS & Android] Delphi 10.1 Berlin - drawing proble ...

  2. ios cocos2d 画线出现闪烁问题

    根据http://www.merowing.info/2012/04/drawing-smooth-lines-with-cocos2d-ios-inspired-by-paper/ 用cocos2d ...

  3. HTML5自学笔记[ 12 ]canvas绘图小示例之鼠标画线

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等

    // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affec ...

  5. [ios]MKMapView中使用MKPolyline画线

    参考:http://blog.sina.com.cn/s/blog_9e8867eb0101dt76.html 首先在MapView.h中 #import <MapKit/MapKit.h> ...

  6. IOS 绘制基本图形( 画圆、画线、画圆弧、绘制三角形、绘制四边形)

    // 当自定义view第一次显示出来的时候就会调用drawRect方法- (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIG ...

  7. IOS简单画板实现

    先上效果图 设计要求 1.画笔能设置大小.颜色 2.有清屏.撤销.橡皮擦.导入照片功能 3.能将绘好的画面保存到相册 实现思路 1.画笔的实现,我们可以通过监听用户的 平移手势 中创建 UIBezie ...

  8. matplotlib画线(2)

    这篇随笔是matplotlib画线的补充>>> #nocl参数控制图例中有几列,>>> import numpy as np>>> import ...

  9. canvas小画板--(1)平滑曲线

    功能需求 项目需求:需要实现一个可以自由书写的小画板 简单实现 对于熟悉canvas的同学来说,这个需求很简单,短短几十行代码就能实现: <!doctype html> <html& ...

随机推荐

  1. 在新机器部署Qt+mysql程序

    1.一般情况下需要将Qt5Core.dll,Qt5Gui.dll,Qt5Sql.dll,Qt5Widgets.dll,platforms\qwindows.dll msvcp120.dll,msvcp ...

  2. 解决xampp端口冲突

    今天电脑按了一下xampp,结果运行wamp的时候,出现了下面的情况: 在网上查了一下,结果是端口被占用,然后我就手动修改了一下. 1.修改xampp的端口号 依次找到:XAMPP的安装目录(比如D: ...

  3. gcd推导

    欧几里得算法有性质: gcd(a, b)=gcd(b, a%b); 那么如何证明呢~ 法1: 我们先假设其成立并且有 gcd(a, b)=gcd(b, a%b)=d; a=k*b+c即a%b=c(我们 ...

  4. 参数max_allowed_packet

    通信信息包是发送至MySQL服务器的单个SQL语句,或发送至客户端的单一行. 当MySQL客户端或mysqld服务器收到大于max_allowed_packet字节的信息包时,将发出“信息包过大”错误 ...

  5. 优雅的数组降维——Javascript中apply方法的妙用

    将多维数组(尤其是二维数组)转化为一维数组是业务开发中的常用逻辑,除了使用朴素的循环转换以外,我们还可以利用Javascript的语言特性实现更为简洁优雅的转换.本文将从朴素的循环转换开始,逐一介绍三 ...

  6. IOS真机测试

    学习了两天的Android开发,我感觉Android开发跟IOS开发和.NET平台下的开发有点不同,Android开发我更觉得跟web(Html)倒是有类似的地方,都是节点标签显示的,当然个人理解,感 ...

  7. js中替换返回json中的空格为&nbsp;

    使用.replace(/\s/g, ' ');来替换空格.在IE中 不起作用,可以指定编码将字体设置为:{font-family: Simsun;}

  8. mysql多表联合查询

    转自:http://www.cnblogs.com/Toolo/p/3634563.html 多表连接,小分三种(笛卡尔积.内连接.外连接),多分五种 (笛卡尔积.内连接.左连接.右连接.全连接(my ...

  9. ArrayList数组列表

    ArrayList数组列表 Collection接口和List接口的区别 List接口扩充了Collection接口,添加了索引相关的方法. code example Object get(int i ...

  10. css变形 transform【转】

    transition:过度属性 transition-property 规定设置过度效果的css属性的名称,默认可以写all transition-duration 规定完成过度效果需要多少秒或毫秒 ...