图形上下文的矩阵操作(平移-缩放-旋转)

图形上下文的矩阵操作(旋转、缩放和平移)
CGContextRotateCTM:图形上下文旋转,以上下文的原点(左上角)为基准
CGContextScaleCTM:图形上下文的缩放,以上下文的原点(左上角)为基准
CGContextTranslateCTM:图形上下文的平移,以上下文的原(左上角)点为基准
 
注意:一定要在添加路径之前进行设置
 
下面贴出swift版代码:
 1 override func draw(_ rect: CGRect) {
 2         let context = UIGraphicsGetCurrentContext()
 3         
 4         // MARK: - 注意:矩阵操作一定要在添加路径之前设置
 5         
 6         // 旋转 - 逆时针旋转-M_PI_4(以原点为基准)
 7         // context?.rotate(by: CGFloat(-M_PI_4))
 8         
 9         // 平移 - 往左下角(以原点为基准)
10         // context?.translateBy(x: 50, y: 50)
11         
12         // 缩放 - 宽高各缩小一半(以原点为基准)
13         context?.scaleBy(x: 0.5, y: 0.5)
14         
15         let borderPath = UIBezierPath(rect: rect)
16         
17         let rectPath = UIBezierPath(rect: CGRect(x: 10, y: 10,  200, height: 200))
18         
19         let circlePath = UIBezierPath(ovalIn: CGRect(x: 10, y: 210,  200, height: 200))
20     
21         context?.addPath(borderPath.cgPath)
22         context?.addPath(rectPath.cgPath)
23         context?.addPath(circlePath.cgPath)
24         
25         context?.setLineWidth(2)
26         
27         context?.strokePath()
28     }
 
原文地址:https://www.cnblogs.com/panda1024/p/6256222.html