swift5 画图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

/*
_____
| /
| /
| /
|/
*/



/// 左上角上角三角形
class TriangleView: UIView {
var stokeColor: UIColor = UIColor.red
var fillColor: UIColor = UIColor.blue

override func draw(_ rect: CGRect) {
// 获取当前的图形上下文
let context = UIGraphicsGetCurrentContext()
// 设置边框颜色
context?.setStrokeColor(stokeColor.cgColor)
// 设置填充颜色
context?.setFillColor(fillColor.cgColor)
// 开始画线,需要将起点移动到指定的point
context?.move(to: CGPoint(x: 0, y: 0))
// 添加一根线到另一个点 (两点一线)
context?.addLine(to: CGPoint(x: rect.maxY, y: 0))
// 添加一根线到另一个点 (两点一线)
context?.addLine(to: CGPoint(x: 0, y: rect.maxX))

// 画线完毕,最后将起点和终点封起来
context?.closePath()
/*
stroke : 边框;
fill : 填充
fillStroke : 边框 + 填充
*/
context?.drawPath(using: .fillStroke)
// 渲染上下文
context?.strokePath()
}
}

let v = TriangleView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))

本文标题:swift5 画图

文章作者:史彦超

发布时间:2019年10月23日 - 15:10

最后更新:2021年07月20日 - 16:07

原始链接:https://doingself.github.io/2019/10/23/swift5-%E7%94%BB%E5%9B%BE/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Donate comment here