码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-01-06 15:27:25      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:代码   nbsp   swift   override   旋转   graphics   lin   border   stroke   

图形上下文的矩阵操作(旋转、缩放和平移)
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, width: 200, height: 200))
18         
19         let circlePath = UIBezierPath(ovalIn: CGRect(x: 10, y: 210, width: 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     }

 

 

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

标签:代码   nbsp   swift   override   旋转   graphics   lin   border   stroke   

原文地址:http://www.cnblogs.com/panda1024/p/6256222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!