框架:CoreGraphics
步骤:
1、“获取”图形上下文 let cxtRef = UIGraphicsGetCurrentContext()!
2、添加路径
3、渲染
--- cxtRef.strokePath() :描边,只画线条
--- cxtRef.fillPath() :填充,负责里面的内容,不管边线
注意:cxtRef.closePath() --- 关闭路径 -> 将路径的终点向起点连线
-----------------------------------------------------------------------------------------------------------------------------------------
直线:
private func zhiXian(){
// 1、“获取”图形上下文对象
let cxtRef = UIGraphicsGetCurrentContext()! // 2、添加直线路径
cxtRef.move(to: CGPoint(x: , y: ))
cxtRef.addLine(to: CGPoint(x: , y: )) // 3、描边渲染
cxtRef.strokePath()
}
三角形:
private func sanJiaoXing(){
// 1、“获取”图形上下文对象
let cxtRef = UIGraphicsGetCurrentContext()! // 2、添加三角形路径
let point1 = CGPoint(x: , y: )
let point2 = CGPoint(x: , y: )
let point3 = CGPoint(x: , y: ) // cxtRef.addLines(between: [point1,point2,point3])
cxtRef.move(to: point1)
cxtRef.addLine(to: point2)
cxtRef.addLine(to: point3) // 3、关闭路径 -> 将路径的终点向起点连线
cxtRef.closePath() // 4、渲染
// stroke - 描边,只画线条
// cxtRef.strokePath() // fill - 填充,负责里面的内容,不管边线
cxtRef.fillPath()
}
矩形:
private func juXing(rect:CGRect){
// 1、获取图形上下文对象
let cxtRef = UIGraphicsGetCurrentContext()! // 2、添加矩形路径
cxtRef.addRect(CGRect(x: , y: , width: , height: )) // 3、描边渲染
cxtRef.strokePath()
}
椭圆形:
private func tuoYuanXing(){
// 1、获取图形上下文对象
let cxtRef = UIGraphicsGetCurrentContext()! // 2、添加椭圆形路径 - 内切圆
cxtRef.addEllipse(in: CGRect(x: , y: , width: , height: )) // 3、描边渲染
cxtRef.strokePath()
}
弧形:
private func huXing(){
// 1、获取图形上下文对象
let cxtRef = UIGraphicsGetCurrentContext()! // 2、添加弧形路径
// clockwise: true - 逆时针;false - 顺时针
cxtRef.addArc(center: CGPoint(x:,y:), radius: , startAngle: , endAngle: CGFloat(M_PI_4), clockwise: false) // 3、描边渲染
cxtRef.strokePath()
}