【发布时间】:2019-09-08 12:29:33
【问题描述】:
当我通过ray wenderlich学习Core Graphic时,
一步是转换UIBezierPath,var transform = CGAffineTransform(scaleX: 0.8, y: 0.8),
我不知道为什么后面的步骤是对的,是transform = transform.translatedBy(x: 15, y: 30)?
不知道x和y位置是怎么计算出来的。
通过打印UIBezierPath currentPoint print(medallionPath.currentPoint),我以为宽度应该是(x1 - x2) * 0.5,高度应该是y1 - y2,我真的不知道为什么会这样
(x: 15, y: 30)
整个代码如下,在 Playground 中测试
let size = CGSize(width: 120, height: 200)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
let context = UIGraphicsGetCurrentContext()!
//Gold colors
let darkGoldColor = UIColor(red: 0.6, green: 0.5, blue: 0.15, alpha: 1.0)
let midGoldColor = UIColor(red: 0.86, green: 0.73, blue: 0.3, alpha: 1.0)
let medallionPath = UIBezierPath(ovalIn: CGRect(x: 8, y: 72, width: 100, height: 100))
print(medallionPath.currentPoint)
// (108.0, 122.0)
print(medallionPath.bounds)
// (8.0, 72.0, 100.0, 100.0)
context.saveGState()
medallionPath.addClip()
darkGoldColor.setFill()
medallionPath.fill()
context.restoreGState()
// question
var transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
// transform = transform.translatedBy(x: 15, y: 30)
medallionPath.lineWidth = 2.0
//apply the transform to the path
medallionPath.apply(transform)
print(medallionPath.currentPoint)
// (86.4, 97.6)
print(medallionPath.bounds)
// (6.4, 57.6, 80.0, 80.0)
medallionPath.stroke()
//This code must always be at the end of the playground
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
【问题讨论】:
标签: ios swift uikit core-graphics drawrect