2

I want to rotate image on angle.But i wnt to rotate image with fix point.How can i set this fix point?

3 Answers3

13

Set the view's layer's anchorpoint, which is in view-local coordinates from 0 to 1. That is, the top left is 0,0 and the bottom right is 1,1.

For example, the default is to rotate around the center:

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

If you want to rotate around the origin:

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

Or the middle right edge:

imageView.layer.anchorPoint = CGPointMake(1,.5);
Ed Marty
  • 39,590
  • 19
  • 103
  • 156
  • For the people using this method, note that you have to include the Quartz2D framework in your project and class to be able to access the layer's properties. – Dimitris Oct 22 '09 at 09:02
  • USELESS! Okay, you have changed anchorPoint. But by this action you have also moved your view/layer. So would you fix it? – Vyachaslav Gerchicov Aug 31 '15 at 08:54
4

If you create a project using the default "View-based Application" in Xcode, just add this code to your -viewDidLoad in the main view controller that it creates:

- (void)viewDidLoad {
    [super viewDidLoad];

    CABasicAnimation *rotationAnimation = [CABasicAnimation 
                         animationWithKeyPath:@"transform.rotation.z"];

    [rotationAnimation setFromValue:DegreesToNumber(0)];
    [rotationAnimation setToValue:DegreesToNumber(360)];
    [rotationAnimation setDuration:3.0f];
    [rotationAnimation setRepeatCount:10000];

    [[[self view] layer] addAnimation:rotationAnimation forKey:@"rotate"];
}

This will rotate the view around the x axis (center by default). You can just set the contents of that view (e.g. [[self view] setContents:image] where image is an image that you load using UIImage).

Here are my helper functions for converting degrees to radians.

CGFloat DegreesToRadians(CGFloat degrees)
{
    return degrees * M_PI / 180;
}

NSNumber* DegreesToNumber(CGFloat degrees)
{
    return [NSNumber numberWithFloat:
            DegreesToRadians(degrees)];
}

Hope that helps.

Matt Long
  • 24,438
  • 4
  • 73
  • 99
1

imageView.layer.anchorPoint = CGPointMake(.5,.5); for get this you need to import following header file

#import <QuartzCore/QuartzCore.h>
Didier Ghys
  • 30,396
  • 9
  • 75
  • 81
ishan
  • 11
  • 1