CoreGraphics-基本图形绘制-直线、三角形、矩形、椭圆形、弧形

框架:CoreGraphics

步骤:

1、“获取”图形上下文     let cxtRef = UIGraphicsGetCurrentContext()!

2、添加路径  

3、渲染

    --- cxtRef.strokePath()  :描边,只画线条

    --- cxtRef.fillPath() :填充,负责里面的内容,不管边线

注意:cxtRef.closePath()  ---  关闭路径 -> 将路径的终点向起点连线

 

-----------------------------------------------------------------------------------------------------------------------------------------

直线:

 1     private func zhiXian(){
 2         // 1、“获取”图形上下文对象
 3         let cxtRef = UIGraphicsGetCurrentContext()!
 4         
 5         // 2、添加直线路径
 6         cxtRef.move(to: CGPoint(x: 60, y: 20))
 7         cxtRef.addLine(to: CGPoint(x: 60, y: 60))
 8         
 9         // 3、描边渲染
10         cxtRef.strokePath()
11     }

三角形:

 1 private func sanJiaoXing(){
 2         // 1、“获取”图形上下文对象
 3         let cxtRef = UIGraphicsGetCurrentContext()!
 4         
 5         // 2、添加三角形路径
 6         let point1 = CGPoint(x: 10, y: 10)
 7         let point2 = CGPoint(x: 10, y: 50)
 8         let point3 = CGPoint(x: 50, y: 50)
 9         
10         // cxtRef.addLines(between: [point1,point2,point3])
11         cxtRef.move(to: point1)
12         cxtRef.addLine(to: point2)
13         cxtRef.addLine(to: point3)
14         
15         // 3、关闭路径 -> 将路径的终点向起点连线
16         cxtRef.closePath()
17         
18         // 4、渲染
19         // stroke - 描边,只画线条
20         // cxtRef.strokePath()
21         
22         // fill - 填充,负责里面的内容,不管边线
23         cxtRef.fillPath()
24     }

矩形:

 1     private func juXing(rect:CGRect){
 2         // 1、获取图形上下文对象
 3         let cxtRef = UIGraphicsGetCurrentContext()!
 4         
 5         // 2、添加矩形路径
 6         cxtRef.addRect(CGRect(x: 10, y: 10, width: 20, height: 20))
 7         
 8         // 3、描边渲染
 9         cxtRef.strokePath()
10     }

椭圆形:

 1     private func tuoYuanXing(){
 2         // 1、获取图形上下文对象
 3         let cxtRef = UIGraphicsGetCurrentContext()!
 4         
 5         // 2、添加椭圆形路径 - 内切圆
 6         cxtRef.addEllipse(in: CGRect(x: 10, y: 10, width: 30, height: 60))
 7         
 8         // 3、描边渲染
 9         cxtRef.strokePath()
10     }

弧形:

 1     private func huXing(){
 2         // 1、获取图形上下文对象
 3         let cxtRef = UIGraphicsGetCurrentContext()!
 4         
 5         // 2、添加弧形路径
 6         // clockwise: true - 逆时针;false - 顺时针
 7         cxtRef.addArc(center: CGPoint(x:50,y:50), radius: 50, startAngle: 0, endAngle: CGFloat(M_PI_4), clockwise: false)
 8         
 9         // 3、描边渲染
10         cxtRef.strokePath()
11     }

 

posted @ 2017-01-05 15:00  小课桌  阅读(707)  评论(0编辑  收藏  举报