Rotate Image iOS Tutorial

The Core Animation framework makes it possible to perform all type animations on a view, including rotation. In this tutorial an image will be rotated. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use IOSRotateImageTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

Go to the Storyboard. Add an Image View from the Object Library to the main view and go to the Size Inspector. Enter the following values to position the Image View.

Select the Resolve Auto Layout Issues button and select Reset to Suggested Constraints.

Download the zip file containing the image. Extract it and drag the file to the Assets Library. In the storyboard select the image View and in the Attribute Inspector add the image name in the Image field.

The Storyboard should look like this.

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl + drag from the Image View to the ViewController class and create the following Outlet.

Ctrl + drag from the Rotate button to the ViewController class and create the following Action.

Go to the ViewController.swift file and implement the rotateImage method.

@IBAction func rotateImage(_ sender: Any) {
    UIView.animate(withDuration: 2.0, animations: {
        self.imageView.transform = CGAffineTransform(rotationAngle: (180.0 * .pi) / 180.0)

The image will rotate 180 degrees using the CGAffineTranform function. Build and Run the project and tap the Rotate Button to rotate the image.

You can download the source code of the IOSRotateImageTutorial at the ioscreator repository on Github