How can I rotate a UIImageView with respect to any point (other than its center)?

17,589

One way of doing this is by changing the anchorPoint of the UIImageView's underlying layer and rotating about that. You can change the point of rotation using something like the following:

imageView.layer.anchorPoint = CGPointMake(0.25, 0.25);

anchorPoint is defined in terms of relative coordinates within the layer. That is, (0,0) is the upper-left of the layer (on the iPhone, where UIView layers have flipped Y coordinates) and (1,1) is the lower-right.

Moving the anchorPoint may move your image, so you might need to adjust its position afterwards.

To rotate the image's layer about that new anchor point, you can change the CATransform3D struct for the layer using something like the following:

CATransform3D rotatedTransform = imageView.layer.transform;
rotatedTransform = CATransform3DRotate(rotatedTransform, 60.0 * M_PI / 180.0, 0.0f, 0.0f, 1.0f);
imageView.layer.transform = rotatedTransform;

This example does an incremental rotation of 60 degrees about the anchor point.

All changes to a layer are animated by default. You can disable this animation by enclosing your changes to these properties in a CATransaction like the following:

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];    

// Code for changes here

[CATransaction commit];
Share:
17,589
Brad Larson
Author by

Brad Larson

I'm currently an engineer at PassiveLogic, building Swift-based machine learning systems for optimizing complex control systems. We envision buildings as giant robots, and believe that more efficient control of these intricate systems can significantly reduce energy demand globally. Formerly, I was a member of the Swift for TensorFlow team at Google, exploring how machine learning could be advanced by integrating first-class support for differentiability into the Swift language. Many items we built there, such as differentiable Swift, live on in open source (differentiable Swift is a key component of our products at PassiveLogic). Prior to that, I was the CEO and cofounder of SonoPlot, Inc. SonoPlot manufactures and sells robotic systems for the printed electronics and biological research markets. I also used to do Mac and iOS development on the side via my one-man company Sunset Lake Software, with a particular focus on the iPhone and iPad. I released an open-source molecular modeler for Mac and iOS called Molecules, and plan to open source my former equation editor called Pi Cubed as well. I'm the author of an open source framework called GPUImage which lets you perform GPU-accelerated filtering of images and video on Mac, iOS, and Linux. I also taught a course on advanced iPhone development at the Madison Area Technical College, for which the fall and spring semesters' videos can be found on iTunes U. Class notes for this course (with links to sample code and other resources) can be downloaded here in VoodooPad format.

Updated on July 18, 2022

Comments

  • Brad Larson
    Brad Larson almost 2 years

    By default, a UIImageView will rotate only about its center. How do I get it to rotate about any other point in the image?

  • Dan Rosenstark
    Dan Rosenstark over 13 years
    Just adding some notes here for my future selves: #import <QuartzCore/CATransform3D.h> #import <QuartzCore/CALayer.h> and you also need to import the QuartzCore framework. Thanks.
  • hfossli
    hfossli almost 13 years
    but this moves the image as you say. Is it possible to just move the anchor point?
  • Brad Larson
    Brad Larson almost 13 years
    @Fossli - No, the position of a layer is tied to its anchor point. However, it's relatively easy to figure out what the new position for the layer should be, and to change both the anchor point and position of the layer in one non-animated transaction.
  • Mohammad Abdurraafay
    Mohammad Abdurraafay over 12 years
    Error: Too few arguments to function call, expected 5, have 4 CATransform3DRotate(60.0 * M_PI / 180.0, 0.0f, 0.0f, 1.0f);
  • Brad Larson
    Brad Larson over 12 years
    @Mohammad Abdurraafay - You're right, I was missing a transform parameter at the start of that function. This has now been corrected.
  • Eric Brotto
    Eric Brotto over 12 years
    Geez Brad, what don't you know :)