Question

I have next task: on button click image have to rotate to 90 degrees.

My code is:

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *vector;
@end

=========

@interface ViewController ()

@end

@implementation ViewController
@synthesize vector;

- (void)viewDidLoad
{
    [super viewDidLoad];
}
- (IBAction)rotateOn:(id)sender {
    [self rotateImage:self.vector duration:2
                curve:UIViewAnimationCurveEaseIn degrees:M_PI/2];
}

- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
              curve:(int)curve degrees:(CGFloat)degrees
{
    // Setup the animation
    [UIView beginAnimations:NULL context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [image.layer setAnchorPoint:CGPointMake(0.5,0.5)];
    CGAffineTransform transform =
    CGAffineTransformMakeRotation(degrees);
    image.transform = transform;
    [UIView commitAnimations];
}




Before click on the button I have this

Before Click


After click this

After Click

You can see that image rotate to 90 degrees. But it moved down from start center point. Also if I click button again nothing happens.

Was it helpful?

Solution

I assume you are using iOS6 and storyboards, try to disable auto layout in your storyboard and test the animation again.
If it works, and want to keep auto layout feature, you will need to adjust your constraints !

By the way, the default anchorPoint is already (0.5, 0.5), the line is not necessary (unless you modify the anchorPoint elsewhere):

[image.layer setAnchorPoint:CGPointMake(0.5,0.5)];

OTHER TIPS

inside ur rotation button method use this code to rotate the image by 90

static int numRot = 0;
myimage.transform = CGAffineTransformMakeRotation(M_PI_2 * numRot);
++numRot;

Remove this line:

  [image.layer setAnchorPoint:CGPointMake(0.5,0.5)];

It doesn't actually do anything as {0.5,0.5} is the default and the centrepoint. You must have been using different values such as {0,0} to get offcentre rotations.

To increment the rotation every time you click the button, you need to use cumulative transforms. The "Make..." transforms are context-free - they do not take previous transforms into account. Instead try this:

CGAffineTransform transform; 
transform = CGAffineTransformRotate(image.transform, degrees);
image.transform = transform;

You are then adding degrees rotation to the current image's transform, rather than resetting the transform to your new value.

Use :

imageView.transform = CGAffineTransformMakeRotation(0);//Upright
imageView.transform = CGAffineTransformMakeRotation(M_PI/2);//90 degrees clockwise
imageView.transform = CGAffineTransformMakeRotation(-M_PI/2);//90 degrees counter-clockwise

also, check your frames

  • before you click the button
  • after clicking the button, before rotation
  • after rotation

You might find anomalies there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top