Tuesday, October 30, 2018

Rotating a View in Swift

In Swift 3, a view or UIView object, has a property called transform, which is of type CGAffineTransform. This property is used to scale or rotate a view's frame rectangle in its superview's coordinate system. The CGAffineTransform struct represents an affine transform matrix used in drawing 2D graphics. Making changes to this property (or assigning a new value to it) triggers a redraw of its view.

Calling button.transform.rotated(by: CGFloat(Float.pi)) does not make any change to the button's transform property. Instead, it constructs a new copy of the property and rotates it. To rotate a button 180 degree, you can call the rotated() method on the existing transform property and assign the returned affine transformation matrix back to it as following:

button.transform = button.transform.rotated(by: CGFloat(Float.pi))

The rotated() method takes a floating-point number as a parameter which is a value of radian.

Radian


Radian is an International System unit used for measuring angles. The value of radian π or 3.14 is converted to an angle of 180 degree.


Affine Transformation


Affine transformation is used to transform an image by preserving points, straight lines, and planes. Examples of an affine transformation are translation, scaling, and rotation. It modifies its matrix to get a new coordinate of each point in an image for transformation.







No comments:

Post a Comment