By default, a UIImageView will rotate only about its center. How do I get it to rotate about any other point in the image?
views:
2740answers:
2One 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(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];
With a UIImageView, you can set its transform property:
CGAffineTransform transform = imageview.transform;
CGPoint center = CGPointMake(centerOfRotationX, centerOfRotationY);
// Rotate the view 90 degrees around its new center point.
transform = CGAffineTransformRotate(transform, rotationInRadians);
imageview.transform = transform;