使用UIBezierPath可以创建基于矢量的路径。使用此类可以定义简单的形状,如椭圆、矩形或者有多个直线和曲线段组成的形状等。
主要用到的该类的属性包括

moveToPoint:  //设置起始点
addLineToPoint: //从上一点连接一条线到本次指定的点
closePath() //闭合路径,把起始点和终点连接起来
appendPath: //多条路径合并
removeAllPoints() //删除所有点和线 lineWidth //路径宽度
lineCapStyle //端点样式(枚举)
lineJoinStyle //连接点样式(枚举) //下面这几个属性要用在UIView中重写drawRect:方法中使用才有效,否则不会出现效果
UIColor.redColor().setStroke() //设置路径颜色(不常用)
stroke()渲染路径
UIColor.redColor().setFill() //设置填充颜色(不常用)
fill()渲染填充部分 注:再次声明mainPath.stroke() 不是去连线的,而是在渲染路径

画直线

let mainPath = UIBezierPath()
mainPath.moveToPoint(CGPointMake(40, 0)) //开始绘制,表示这个点是起点
mainPath.addLineToPoint(CGPointMake(40, 100))
mainPath.removeAllPoints() //删除所有点和线

画圆弧(兼职画圆)

/*
参数解释:
1.center: CGPoint 中心点坐标
2.radius: CGFloat 半径
3.startAngle: CGFloat 起始点所在弧度
4.endAngle: CGFloat 结束点所在弧度
5.clockwise: Bool 是否顺时针绘制
7.画圆时,没有坐标这个概念,根据弧度来定位起始点和结束点位置。M_PI即是圆周率。画半圆即(0,M_PI),代表0到180度。全圆则是(0,M_PI*2),代表0到360度
*/
let mainPath1 = UIBezierPath(arcCenter: CGPoint(x: 50, y: 50), radius: 50, startAngle: CGFloat(M_PI) * 0, endAngle: CGFloat(M_PI) * 2, clockwise: true)

除了直接初始化一个圆弧,也可以增加一段圆弧路径(mainPath1.addCurveToPoint:)

初始化时画圆

let mainPath2 = UIBezierPath(ovalInRect: CGRect(origin: CGPoint(x: 50, y: 50), size: CGSize(width: 30, height: 30)))

画赛贝尔曲线
贝塞尔线是用于主要用于绘制路径及帧动画,我们简单的看下用法,不做深究
详细资料:http://www.cnblogs.com/jay-dong/archive/2012/09/26/2704188.html

//二阶贝塞尔曲线
let mainPath3 = UIBezierPath()
mainPath3.moveToPoint(CGPointMake(0, 0))
mainPath3.addQuadCurveToPoint(CGPoint(x: 40, y: 0), controlPoint: CGPoint(x: 20, y:50)) //三阶贝塞尔曲线
let mainPath4 = UIBezierPath()
mainPath4.moveToPoint(CGPointMake(0, 0))
mainPath4.addCurveToPoint(CGPoint(x: 120, y: 0), controlPoint1: CGPoint(x: 40, y: 80), controlPoint2: CGPoint(x: 80, y: -80))

三角形

let mainPath5 = UIBezierPath()
mainPath5.moveToPoint(CGPointMake(40, 0))
mainPath5.addLineToPoint(CGPointMake(0, 40))
mainPath5.addLineToPoint(CGPointMake(80, 40))
mainPath5.closePath() //闭合路径,连线结束后会把起点和终点连起来

矩形

//实例化时建立矩形
let mainPath6 = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 40, height: 60)) //实例化带圆角矩形
let mainPath7 = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 40, height: 40), cornerRadius: 10) //实例化单角圆弧矩形
let mainPath8 = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 40, height: 40), byRoundingCorners: UIRectCorner.TopLeft, cornerRadii: CGSize(width: 10, height: 10)) //用路径方法画正方形
let mainPath9 = UIBezierPath()
mainPath9.moveToPoint(CGPointMake(0, 0))
mainPath9.addLineToPoint(CGPointMake(80, 0))
mainPath9.addLineToPoint(CGPointMake(80, 80))
mainPath9.addLineToPoint(CGPointMake(0, 80))
mainPath9.closePath() //和三角形一样需要闭合起点和终点
UIColor.redColor().setFill() //设置填充色
mainPath9.fill()

//多条路径合并

let mainPath10 = UIBezierPath()
mainPath10.moveToPoint(CGPointMake(0, 0))
mainPath10.addLineToPoint(CGPointMake(0, 80)) let mainPath11 = UIBezierPath()
mainPath11.moveToPoint(CGPointMake(0, 80))
mainPath11.addLineToPoint(CGPointMake(40, 40)) mainPath10.appendPath(mainPath11)//多条路径合并

//CAShapeLayer,可以看做一个动画容器。把UIBezierPath绘制的路径放进去,点就会沿着这路径前进,加上颜色、动画等渲染后显示在界面上

let shapeLayer = CAShapeLayer()
shapeLayer.path = mainPath11.CGPath //存入UIBezierPath的路径
shapeLayer.fillColor = UIColor.clearColor().CGColor //设置填充色
shapeLayer.lineWidth = 2 //设置路径线的宽度
shapeLayer.strokeColor = UIColor.grayColor().CGColor //路径颜色
//如果想变为虚线设置这个属性,[实线宽度,虚线宽度],若两宽度相等可以简写为[宽度]
shapeLayer.lineDashPattern = [2]
//一般可以填"path" "strokeStart" "strokeEnd" 具体还需研究
let baseAnimation = CABasicAnimation(keyPath: "strokeEnd")
baseAnimation.duration = 2 //持续时间
baseAnimation.fromValue = 0 //开始值
baseAnimation.toValue = 2 //结束值
baseAnimation.repeatDuration = 5 //重复次数
shapeLayer.addAnimation(baseAnimation, forKey: nil) //给ShapeLayer添
//显示在界面上
self.view.layer.addSublayer(shapeLayer)
文/Mi欧阳(简书作者)
原文链接:http://www.jianshu.com/p/5abd2da87e94
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

Swift之UIBezierPath的更多相关文章

  1. Swift用UIBezierPath来画圆角矩形、自定义多路径图形

    最好的特点就是可以自定义路径,设置圆角和描边都很方便,以下为代码和效果,均在playground中实现 1.首先实现一个圆角矩形,并对此路径描边,为其绘制一个轮廓. 1 2 3 4 5 6 7 8 9 ...

  2. Swift与Objective-C API的交互

    互用性是让 Swift 和 Objective-C 相接合的一种特性,使你能够在一种语言编写的文件中使用另一种语言.当你准备开始把 Swift 融入到你的开发流程中时,你应该懂得如何利用互用性来重新定 ...

  3. Swift - UIBezierPath

    使用UIBezierPath可以创建基于矢量的路径.使用此类可以定义简单的形状,如椭圆.矩形或者有多个直线和曲线段组成的形状等.主要用到的该类的属性包括 moveToPoint: //设置起始点 ad ...

  4. Swift - LineChart绘制折线图

    LineChart,就使用Core Graphics和QuartzCore框架中的CAShapeLayer绘制.这样执行效率明显比堆砌UIView的方法效率高--占用资源少,执行快. 看看CALaye ...

  5. Swift: 深入理解Core Animation(一)

    如果想在底层做一些改变,想实现一些特别的动画,这时除了学习Core Animation之外,别无选择. 最近在看<iOS Core Animation:Advanced Techniques&g ...

  6. [翻译]使用Swift在Xcode中创建自定义控件

    使用Swift在Xcode中创建自定义控件 原文 IBDesignable and IBInspectable With IBDesignable and IBInspectable, develop ...

  7. iOS复杂动画之抽丝剥茧(Objective-C & Swift)

    一.前言 随着开发者的增多和时间的累积,AppStore已经有非常多的应用了,每年都有很多新的APP产生.但是我们手机上留存的应用有限,所以如何吸引用户,成为产品设计的一项重要内容.其中炫酷的动画效果 ...

  8. iOS开发——图形编程Swift篇&CAShapeLayer实现圆形图片加载动画

    CAShapeLayer实现圆形图片加载动画 几个星期之前,Michael Villar在Motion试验中创建一个非常有趣的加载动画. 下面的GIF图片展示这个加载动画,它将一个圆形进度指示器和圆形 ...

  9. Swift—Core Foundation框架-备

    Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.虽然在Swift中调用这种C语言风格的API比较麻烦,但是在OS X和iOS开发 ...

随机推荐

  1. 【工作备忘】suricata

    因为工作遇到的困难,我向suricata的某个作者发送了邮件. On Wed, Sep 11, 2013 at 8:22 AM, likeyi <929812468@qq.com> wro ...

  2. Array.prototype.slice.call

    Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组 ,::'age'}; Array.prototype.slice.call(arr); ...

  3. cocos2d-x 3.2读取xml和json练习

    读取和生成xml文件: #include "tinyxml2/tinyxml2.h" using namespace tinyxml2; void HelloWorld::make ...

  4. MYSQL数据库性能调优之六:备份

    增量备份

  5. 关于JDBC 连接Access 数据库

    ************连接方式(一)Access_JDBC30.jar,此包由于是免费的,所有限制连接单次不超过50************************* Connection conn ...

  6. [SQL]sql语句bug

    sql语句格式必须严格检查,一个空格的错误都会导致执行错误. 常见有 1:字符串的值要用  ‘  ’  括起来 2:用 , 分隔语句段 3:切记判断条件前有一空格:eg:这里最容易出错 4:属性名是否 ...

  7. iOS7 各种问题解决

    1 UITableView 行分割线不到头,短线问题 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { ...

  8. Find mac address

    Windows Method 1: Using the Command Prompt 1 Click on the Start button.   2 Type cmd in the search b ...

  9. OC: 类的扩展、类的延展、协议、 NSDate

      NSDateFormatter 指定⽇日期格式: NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter ...

  10. WCF 客户端调用几种方式

    http://www..com/html/blogs/20130413/2116.htm CSDN123 http://developer.51cto.com/art/200911/161465.ht ...