一:基本画线:

使用贝赛尔曲线画:

//创建路径

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. js 中 Math对象

    Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法.这是它与Date,String对象的区别. Math 对象属性 Math 对象方法

  2. js: 从setTimeout说事件循环模型

    一.从setTimeout说起 setTimeout()方法不是ecmascript规范定义的内容,而是属于BOM提供的功能.查看w3school对setTimeout()方法的定义,setTimeo ...

  3. Centos7 修改mysql指定用户的密码

    1.登陆mysql或者mariadb(两种任选其一) [root@localhost ~]# mysql -u root [root@localhost ~]# mysql -uroot -p 2.切 ...

  4. Socket通信原理探讨(C++为例)

    一.网络中进程之间如何通信? 本地的进程间通信(IPC)有很多种方式,但可以总结为下面4类: 1.消息传递(管道.FIFO.消息队列) 2.同步(互斥量.条件变量.读写锁.文件和写记录锁.信号量) 3 ...

  5. 给Jquery动态添加的元素添加事件

    给Jquery动态添加的元素添加事件 来源:[http://wangqixia.diandian.com/post/2011-05-10/6597866] 我想很多人都会向我一样曾经 被新元素的事件绑 ...

  6. Windows 8.1安装 Vmware10

    之前在windows 8上安装的Vmware 9.0,已经激活了用的蛮好,可是自从上次自动更新系统到windows 8.1后,启动虚拟机时提示要激活 使用各种激活码与注册机都无效,就算注册表信息丢失但 ...

  7. GO语言中json与map的转换

    直接上代码(需要引入encoding/json包) // 当前程序的包名 package main // 导入其它的包 import ( "encoding/json" " ...

  8. JS截字符串处理数字,汉字,英文问题

    <script> function suolve( str,sub_length ){ var temp1 = str.replace(/[^\x00-\xff]/g,"**&q ...

  9. 【Junit 报错】No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).

    Junit报错 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvi ...

  10. Ajax验证用户名是否存在模板

    Jsp 页面 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8 ...