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
After click this
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.
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)];