一:基本画线:

使用贝赛尔曲线画:

//创建路径

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. c语言中(*p)[n]和*p[n]的区别

    写于2016年12月5日. c语言中(*p)[n]表示的数组指针,在该表达式中按照运算的优先级,首先计算()的中*p,在和[n]计算.含义为指向含有n个元素的一维数组. *p[n]表示的是指针数组,在 ...

  2. Linux常用命令学习8---(用户和用户组管理)

    1.用户和用户组     用户和用户组概念        用户:使用操作系统的人(Linux支持多个用户在同一时间登陆同一个操作系统)        用户组:具有相同权限的一组用户(Linux系统中可 ...

  3. c#中文件与二进制流文件的转换

    将文件转换成二进制方法: /// <summary>    /// 将文件转换成二进制    /// </summary>    /// <param name=&quo ...

  4. Andriod学习笔记1:代码优化总结1

    多行变一行 比如说开发一个简单的计算器应用程序,需要定义0-9的数字按钮,第一次就习惯性地写出了如下代码: Button btn0; Button btn1; Button btn2; Button ...

  5. hihoCoder1388 Periodic Signal(2016北京网赛F:NTT)

    题目 Source http://hihocoder.com/problemset/problem/1388 Description Profess X is an expert in signal ...

  6. Codeforces713C Sonya and Problem Wihtout a Legend(DP)

    题目 Source http://codeforces.com/problemset/problem/713/C Description Sonya was unable to think of a ...

  7. Web设计师值得收藏的10个jQuery特效

    jQuery已经不是什么新鲜的事儿,以前总把它认为是非常难的东西,也就没有认真去了解他了.直到学完CSS的大部分内容,才开始接触这种"write less, do more" 的J ...

  8. PHP实现快速排序、插入排序、选择排序

    1.快速排序 快速排序(Quicksort)是对冒泡排序的一种改进.由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都 ...

  9. T4学习资料

    网址:http://www.olegsych.com/2007/12/text-template-transformation-toolkit/

  10. web前端~~浏览器兼容问题(百度)

    所谓的浏览器兼容性问题,是指因为不同的浏览器对同一段代码有不同的解析,造成页面显示效果不统一的情况.在大多数情况下,我们的需求是,无论用户用什么浏览器来查看我们的网站或者登陆我们的系统,都应该是统一的 ...