如何在Swift中应用多个变换

66
我想对一个UIView (或其子类) 应用多个转换,例如平移,旋转和缩放。我知道可以使用CGAffineTransformConcat来应用两个变换,但如果我有三个或更多的变换怎么办?
我看过这些问题: 但是这些问题问的是不同的问题,并且给出的答案只讨论了如何使用CGAffineTransformConcat应用两个变换。此外,它们使用Objective-C而不是Swift。
3个回答

158
你可以通过将多个变换叠加在一起来应用它们。
var t = CGAffineTransform.identity
t = t.translatedBy(x: 100, y: 300)
t = t.rotated(by: CGFloat.pi / 4)
t = t.scaledBy(x: -1, y: 2)
// ... add as many as you want, then apply it to to the view
imageView.transform = t

或者更紧凑(但不一定易读):

imageView.transform = CGAffineTransform.identity.translatedBy(x: 100, y: 300).rotated(by: CGFloat.pi / 4).scaledBy(x: -1, y: 2)

这一系列的转换会生成右侧的图像:

enter image description here

感谢这个答案教我如何做。

注释

  • The order in which you apply the transforms matters. For example, if the transforms were done in the opposite order it would produce the following result.

    t = t.scaledBy(x: -1, y: 2)
    t = t.rotated(by: CGFloat.pi / 4)
    t = t.translatedBy(x: 100, y: 300)
    

enter image description here

参见

此答案已经通过 Swift 4 的测试


33

在 Swift 3 中,这些已被替换为 CGAffineTransform 自身的函数,并且可以链接在一起。

extension CGAffineTransform {
    public func translatedBy(x tx: CGFloat, y ty: CGFloat) -> CGAffineTransform
    public func scaledBy(x sx: CGFloat, y sy: CGFloat) -> CGAffineTransform
    public func rotated(by angle: CGFloat) -> CGAffineTransform
}

所以例如

let transform = CGAffineTransform(scaleX: 1.0, y: 3.0).translatedBy(x: 12, y: 9).rotated(by: 17.0)

0

诀窍在于view.transform.translatedBy(x: 100, y: 100)并没有改变view.transform。它只是返回了新的CGAffineTransform,你需要将其重新赋值给view.transform

view.transform = view.transform.translatedBy(x: 100, y: 100)

您可以根据需要多次执行此操作或按顺序执行

view.transform = view.transform.translatedBy(x: 100, y: 100).rotated(by: CGFloat.pi / 2).scaledBy(x: 2, y: 2)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接