There are Key-Value Coding extensions in Core Animation, as documented here, that let you set the z rotation of a layer (or the rotation around any of the 3 3D axes).
That is handy if you want to add an @IBInspectable property to a view that lets you set it's rotation angle in Interface Builder, as I demonstrated in this SO answer.
That code looks like this:
@IBDesignable class RotatableView: UIView {
@objc @IBInspectable var rotationDegrees: Float = 0 {
didSet {
print("Setting angle to \(rotationDegrees)")
let angle = NSNumber(value: rotationDegrees / 180.0 * Float.pi)
layer.setValue(angle, forKeyPath: "transform.rotation.z")
}
}
However, if the layer's transform is "skewed", the angle transform won't work correctly.
UIViews have a property transform, of type CGAffineTransform. That is a 3x3 transformation matrix that only applies 2-dimensional transforms to a view.
It would be cleaner if I could apply a rotation to the view's existing transform property, rather than applying a z rotation to the transform of the view's backing layer. However, I can't figure out how to change the rotation angle of the view's transform using a keyPath.
It might not be possible if the system's Key-Value Coding Extensions don't expose that property. Alternatively, what is the math to set a CGAffineTransform's rotation angle to a specific angle? (I don't want to append more rotation to an existing transform. Nor do I want to compute a transform by applying a rotation to the identity transform. I want to take the view's existing transform and replace it's rotation value with a new rotation value, like the code above does to the layer's CATransform3D.)