使用UIBezierPath可以創(chuàng)建基于矢量的路徑。使用此類可以定義簡單的形狀,如橢圓、矩形或者有多個直線和曲線段組成的形狀等。
主要用到的該類的屬性包括
moveToPoint: //設置起始點
addLineToPoint: //從上一點連接一條線到本次指定的點
closePath() //閉合路徑,把起始點和終點連接起來
appendPath: //多條路徑合并
removeAllPoints() //刪除所有點和線
lineWidth //路徑寬度
lineCapStyle //端點樣式(枚舉)
lineJoinStyle //連接點樣式(枚舉)
//下面這幾個屬性要用在UIView中重寫drawRect:方法中使用才有效,否則不會出現(xiàn)效果
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() //刪除所有點和線
畫圓弧(兼職畫圓)
/*
參數(shù)解釋:
1.center: CGPoint 中心點坐標
2.radius: CGFloat 半徑
3.startAngle: CGFloat 起始點所在弧度
4.endAngle: CGFloat 結(jié)束點所在弧度
5.clockwise: Bool 是否順時針繪制
7.畫圓時,沒有坐標這個概念,根據(jù)弧度來定位起始點和結(jié)束點位置。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() //閉合路徑,連線結(jié)束后會把起點和終點連起來
矩形
//實例化時建立矩形
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 //路徑顏色
//如果想變?yōu)樘摼€設置這個屬性,[實線寬度,虛線寬度],若兩寬度相等可以簡寫為[寬度]
shapeLayer.lineDashPattern = [2]
//一般可以填"path" "strokeStart" "strokeEnd" 具體還需研究
let baseAnimation = CABasicAnimation(keyPath: "strokeEnd")
baseAnimation.duration = 2 //持續(xù)時間
baseAnimation.fromValue = 0 //開始值
baseAnimation.toValue = 2 //結(jié)束值
baseAnimation.repeatDuration = 5 //重復次數(shù)
shapeLayer.addAnimation(baseAnimation, forKey: nil) //給ShapeLayer添
//顯示在界面上
self.view.layer.addSublayer(shapeLayer)