Rotate an ImageView

In iOS you can use transforms to make all kind of animation, including rotating. In this tutorial we will rotate a clock image using the transform property of this imageView.

Update: Sep 30, 2014. The rewritten tutorial in Swift for iOS 8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use RotatingImageViewDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards checkbox is deselected and Use Automatic Reference Counting checkbox is selected.

For this project we'll need a image to let it rotate. Download our picture of our clock here and unpack and add this file to the project.  Go to ViewController.m and add a property in the @interface section.

@property (nonatomic, strong) UIImageView *imageView;

This property will contain our image.In ViewController.m change ViewDidLoad in

- (void)viewDidLoad 
[super viewDidLoad];

UIImage *image = [UIImage imageNamed:@"clock.png"];

self.imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:self.imageView];

self.view.backgroundColor = [UIColor whiteColor];

Our image is loaded and initialized and added to the main view. Add the viewDidAppear method.

UIUI- (void)viewDidAppear:(BOOL)animated 
{ =;

  [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:4.0f]; [UIView setAnimationDelegate:self];

  self.imageView.transform = CGAffineTransformMakeRotation((120.0f * M_PI) / 180.0f);

  [UIView commitAnimations]; 

In viewDidAppear we'll specify the animation code.It uses beginAnimations:context to start an animation block. The animation is set with a duration of 4.0 seconds. Then the transform proprerty is set to a clockwise rotation of 120 degrees using the CGAFffineTransformMakeRotation method. Last, we call commitAnimations to start the animation. Build and Run, you should see the clock image rotating.

You can download the source code of the RotatingImageViewDemo at the ioscreator repository on github.