How to apply multiple transforms in Swift

Solution 1:

You can apply multiple transforms by stacking them on top of each other.

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

Or more compactly (but not necessarily as readable):

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

This series of transforms produces the image on the right:

enter image description here

Thanks to this answer for teaching me how to do it.

Notes

  • 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

See also

  • CGAffineTransform Reference (docs)
  • Transforms (docs)
  • Swift: Translating and Rotating a CGContext, A Visual Explanation (iOS/Xcode)
  • Demystifying CGAffineTransform

This answer has been tested with Swift 4

Solution 2:

In Swift 3, these have been replaced by functions on CGAffineTransform itself, which can be chained.

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
}

so for example

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