Search code examples
swiftswift5cgaffinetransformcgrect

CGAffineTransform Does Not Work With CGRect


CGAffineTransform does not transform CGRect.

My code:

        var rect = CGRect(x: 20, y: 20, width: 300, height: 300)
        let transform = CGAffineTransform(scaleX: 3, y: 3)
        rect.applying(transform)

x,y should be 60, however,they stay the same (20,20) after transform is applied. This is evident both in print(rect.origin.x, rect.origin.y) and in the image I'm cropping with rect.

What am I missing?


Solution

  • applying is not mutating. You didn't reassign.

    rect = rect.applying(transform)