Search code examples
iosswiftuikitcgaffinetransform

How do I flip and rotate a label at the same time?


So I have been working on this project for a while now and it requires me to rotate and flip a label.

Here is what I have:

@IBAction func Flip(_ sender: Any) {
    UIView.animate(withDuration: 2.0,animations: {
        self.timeLabel.transform = CGAffineTransform(scaleX: -1, y: 1)
        self.timeLabel.transform = CGAffineTransform(rotationAngle: (-90 * .pi / 180.0)

I expected to have the label flipped(mirrored) and then rotated when I tap the button"Flip", but the result in the simulator was the rotate only, not the flip.

It would be great if someone could rewrite this code for me so it could do both. I'm pretty new and it seems as though this project will take a while.


Solution

  • As @rmaddy has suggested, prepare two transform separately, concat them together which ends up creating a single transformation, then assign it woulf look like

    let flipping = CGAffineTransform(scaleX: -1, y: 1)
    let rotating = CGAffineTransform(rotationAngle: (-90 * .pi / 180.0)
    let fullTransformation = flipping.concatenating(rotating)
    self.timeLabel.transform = fullTransformation
    

    For more info on the concatenating, check the doc following https://developer.apple.com/documentation/coregraphics/cgaffinetransform/1455996-concatenating