Search code examples
iosswifttransformcore-animationcalayer

Change a CALayer's rotation transform to a value without changing the layer's scale transform


Suppose you have a CALayer subclass as part of your view hierarchy, which has been created with a scale and rotation transform, where the angle and scale is only defined at runtime, i.e:

let layer = CALayer()
layer.frame = CGRect(x: 10, y: 10, width: 100, height: 100)

//where xx and yy are some precomputed values
layer.transform = CATransform3DConcat(CATransform3DMakeScale(xx, xx, 1), 
                                      CATransform3DMakeRotation(yy, 0, 0, 1)) 

let view = UIView() //your view hierarchy
view.addSublayer(layer)

Later in the program, I want to rotate this layer to a specific angle. How do I specify the transform so that

  • A) The original scale applied to the layer is preserved
  • B) The original rotation applied to the layer is discarded and reset to the new value?
let angle = CGFloat.pi/3
layer.transform = ???

Solution

  • You can reapply your transform again having xx value unchanged

        func rotateLayer(by angle: CGFloat) {
            layer.transform = CATransform3DConcat(CATransform3DMakeScale(xx, xx, 1),
            CATransform3DMakeRotation(angle, 0, 0, 1))
        }
    

    Edit

    Based on this answer you can get and reuse uniform scale factor like this

        func rotateLayer(by angle: CGFloat) {
            let scale = sqrt(pow(layer.transform.m11, 2) + pow(layer.transform.m12, 2))
            print(scale)
            layer.transform = CATransform3DConcat(CATransform3DMakeScale(scale, scale, 1), CATransform3DMakeRotation(angle, 0, 0, 1))
        }