使用UIBezierPath可以創(chuàng)建基于矢量的路徑态秧。使用此類可以定義簡(jiǎn)單的形狀摧扇,如橢圓铐达、矩形或者有多個(gè)直線和曲線段組成的形狀等秧饮。
主要用到的該類的屬性包括
moveToPoint: //設(shè)置起始點(diǎn)
addLineToPoint: //從上一點(diǎn)連接一條線到本次指定的點(diǎn)
closePath() //閉合路徑凤类,把起始點(diǎn)和終點(diǎn)連接起來(lái)
appendPath: //多條路徑合并
removeAllPoints() //刪除所有點(diǎn)和線
lineWidth //路徑寬度
lineCapStyle //端點(diǎn)樣式(枚舉)
lineJoinStyle //連接點(diǎn)樣式(枚舉)
//下面這幾個(gè)屬性要用在UIView中重寫drawRect:方法中使用才有效穗泵,否則不會(huì)出現(xiàn)效果
UIColor.redColor().setStroke() //設(shè)置路徑顏色(不常用)
stroke()渲染路徑
UIColor.redColor().setFill() //設(shè)置填充顏色(不常用)
fill()渲染填充部分
注:再次聲明mainPath.stroke() 不是去連線的,而是在渲染路徑
畫直線
let mainPath = UIBezierPath()
mainPath.moveToPoint(CGPointMake(40, 0)) //開(kāi)始繪制谜疤,表示這個(gè)點(diǎn)是起點(diǎn)
mainPath.addLineToPoint(CGPointMake(40, 100))
mainPath.removeAllPoints() //刪除所有點(diǎn)和線
畫圓弧(兼職畫圓)
/*
參數(shù)解釋:
1.center: CGPoint 中心點(diǎn)坐標(biāo)
2.radius: CGFloat 半徑
3.startAngle: CGFloat 起始點(diǎn)所在弧度
4.endAngle: CGFloat 結(jié)束點(diǎn)所在弧度
5.clockwise: Bool 是否順時(shí)針繪制
7.畫圓時(shí)佃延,沒(méi)有坐標(biāo)這個(gè)概念,根據(jù)弧度來(lái)定位起始點(diǎn)和結(jié)束點(diǎn)位置夷磕。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)
除了直接初始化一個(gè)圓弧尺棋,也可以增加一段圓弧路徑(mainPath1.addCurveToPoint:)
初始化時(shí)畫圓
let mainPath2 = UIBezierPath(ovalInRect: CGRect(origin: CGPoint(x: 50, y: 50), size: CGSize(width: 30, height: 30)))
畫賽貝爾曲線
貝塞爾線是用于主要用于繪制路徑及幀動(dòng)畫,我們簡(jiǎn)單的看下用法绵跷,不做深究
詳細(xì)資料: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é)束后會(huì)把起點(diǎn)和終點(diǎn)連起來(lái)
矩形
//實(shí)例化時(shí)建立矩形
let mainPath6 = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 40, height: 60))
//實(shí)例化帶圓角矩形
let mainPath7 = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 40, height: 40), cornerRadius: 10)
//實(shí)例化單角圓弧矩形
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() //和三角形一樣需要閉合起點(diǎn)和終點(diǎn)
UIColor.redColor().setFill() //設(shè)置填充色
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,可以看做一個(gè)動(dòng)畫容器陡鹃。把UIBezierPath繪制的路徑放進(jìn)去,點(diǎn)就會(huì)沿著這路徑前進(jìn)抖坪,加上顏色萍鲸、動(dòng)畫等渲染后顯示在界面上
let shapeLayer = CAShapeLayer()
shapeLayer.path = mainPath11.CGPath //存入U(xiǎn)IBezierPath的路徑
shapeLayer.fillColor = UIColor.clearColor().CGColor //設(shè)置填充色
shapeLayer.lineWidth = 2 //設(shè)置路徑線的寬度
shapeLayer.strokeColor = UIColor.grayColor().CGColor //路徑顏色
//如果想變?yōu)樘摼€設(shè)置這個(gè)屬性,[實(shí)線寬度擦俐,虛線寬度]脊阴,若兩寬度相等可以簡(jiǎn)寫為[寬度]
shapeLayer.lineDashPattern = [2]
//一般可以填"path" "strokeStart" "strokeEnd" 具體還需研究
let baseAnimation = CABasicAnimation(keyPath: "strokeEnd")
baseAnimation.duration = 2 //持續(xù)時(shí)間
baseAnimation.fromValue = 0 //開(kāi)始值
baseAnimation.toValue = 2 //結(jié)束值
baseAnimation.repeatDuration = 5 //重復(fù)次數(shù)
shapeLayer.addAnimation(baseAnimation, forKey: nil) //給ShapeLayer添
//顯示在界面上
self.view.layer.addSublayer(shapeLayer)