Rotating Image Tutorial in iOS8 with Swift

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 built with Xcode 6 and iOS 8.

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftRotatingViewTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard. In Xcode 6 view controllers are now square by default, so that you can create one user interface for multiple devices. In iOS8 you can layout the Storyboard using constraints and size classes. Since this app is only going to be for the iPhone, you can disable size classes. In the File Inspector untick Use Size Classes.

Choose Keep size class data for: iPhone, and click Disable Size Classes.

Add an Image View to the main view and go to the Size Inspector. Enter the following values to position the Image View.

Download the zip file containing the image. Extract it and add the file to the project. Make sure you check the "Copy items if needed" checkbox.

Drag a Button to the main view and place it below the Image View. Give it a title of "Rotate". 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.

Implement the rotateImage method

@IBAction func rotateImage(sender: UIButton) {
    UIView.animateWithDuration(2.0, animations: {
      self.imageView.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)

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

You can download the source code of the iOS8Swift..Tutorial at the ioscreator repository on Github