-
Notifications
You must be signed in to change notification settings - Fork 472
Using View Transforms
There are 3 types of 2 dimensional transforms that you can apply to views: translation, scale, and rotation. You can also use the view transform property to set a 3D perspective transform.
To apply a scale transform, create it as below. In this example, the view will scale up by 2 times.
view.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
view.transform = view.transform.scaledBy(x: 0.5, y: 0.5)
To apply a rotation transform, create it as below. Since the units are in radians, you will need to convert from degrees. In this example, the view will rotate by 45 degrees. You also need to convert it to be of type CGFloat, which is the original Core Graphics type.
view.transform = CGAffineTransform(rotationAngle: CGFloat(45 * M_PI / 180))
or using Swift 3 syntax:
view.transform = view.transform.rotated(by:rotation)
view.transform = view.transform.rotated(by angle: CGFloat(45 * M_PI / 180))
To apply a translation transform, create it as below. In this example, the view will offset by 50 pixels in both the X and Y axis.
view.transform = CGAffineTransform(translationX: 50, y: 50)
view.transform = view.transform.translatedBy(x: 10, y: 10)
In the examples above, we were applying a single transform to a view. Sometimes, you want to be able to apply multiple transforms simultaneously. Each of the transform methods above has another method that will take an existing transform and additively apply a new transform.
For example, to scale and rotate, you can first scale it, then add the rotation transform (or vice versa)
view.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
view.transform = view.transform.rotated(by angle: CGFloat(45 * M_PI / 180))
An easy way to remove any transforms applied is to simply set the transform to the Identity or no transform state. For example,
view.transform = CGAffineTransform.identity
will remove any previous transformations and the view will appear as it had before.