[英]CGAffineTransform to scale width and height only
How to scale a CGontext without affecting its origin ie only the width and height should get scaled? 如何在不影响其原点的情况下缩放CGontext,即只扩展宽度和高度? If I use scale like the below one directly it scales the origin also.
如果我直接使用如下所示的比例,它也会缩放原点。
context.scaledBy(x: 2.0, y: 2.0)
Is there a way to construct an AffineTransform that manipulates the width and height, leaving the origin untouched? 有没有办法构造一个操纵宽度和高度的AffineTransform,保持原点不受影响?
I want an AffineTransform that can be used to both CGContext
and CGRect
. 我想要一个可以用于
CGContext
和CGRect
。
For example a CGRect rect = {x, y, w, h}
例如,
CGRect rect = {x, y, w, h}
var t = CGAffineTransform.identity
t = t.scaledBy(x: sx, y: sy)
let tRect = rect.applying(t)
tRect
will be {x * sx, y * sy, w * sx, h * sy}
tRect
将为{x * sx, y * sy, w * sx, h * sy}
But I want {x, y, w * sx, h * sy}
. 但我想要
{x, y, w * sx, h * sy}
。 Though it can be achieved by calculation, I need CGAffineTransform to do this. 虽然可以通过计算实现,但我需要CGAffineTransform来完成这项工作。
You need to translate the origin, then scale, then undo the translation: 您需要翻译原点,然后缩放,然后撤消翻译:
import Foundation
import CoreGraphics
let rect = CGRect(x: 1, y: 2, width: 3, height: 4) // Whatever
// Translation to move rect's origin to <0,0>
let t0 = CGAffineTransform(translationX: -rect.origin.x, y: -rect.origin.y)
// Scale - <0,0> will not move, width & height will
let ts = CGAffineTransform(scaleX: 2, y: 3) // Whatever
// Translation to restore origin
let t1 = CGAffineTransform(translationX: rect.origin.x, y: rect.origin.y)
//Compound transform:
let t = t0.concatenating(ts).concatenating(t1)
// Test it:
let tRect = rect.applying(t) // 1, 2, 6, 12 as required
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.