Why Won't My Image Rotate in Xcode?
I'm trying to do a simple animation that will rotate an image continuously when a button is clicked, but for some reason when I click the button it crashes the app.
Here's my code.
.h file:
@interface MainView : UIView {
}
IBOutlet UIImageView *sunray;
- (IBAction)pushrotate:(id)sender;
@end
.m file:
@implementation MainView
- (IBAction)pushrotate:(id)sender {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0];
sunray.transform = CGAffineTransformMakeRotation(6.2831855);
[UIView commitAnimations];
}
-(void) dealloc {
[sunray dealloc];
[sunray release];
[super 开发者_StackOverflow社区dealloc];
}
@end
Any idea how I can make it work? Also, what is the best way to make the animation start as soon as the app loads?
If you set your view rotation angle a multiply of 2*PI (it looks so in your case), then the view recognises that transformation does not actually have an effect and view is not rotated.
To rotate your view by angle more then 2*PI with animation you need to use CoreAnimation. So your code should look smth like:
//link with QuartzCore.framework
#import <QuartzCore/QuartzCore.h>
...
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 1.0f;
[sanray.layer addAnimation:animation forKey:@"MyAnimation"];
You probably wired the action or the outlet up wrong (if you posted more console output, we would be sure). See if Interface Builder has any error markers on your actions/outlets.
Also see my comment about calling dealloc yourself - you shouldn't, except for [super dealloc] in your own dealloc implementation.
here is my solution to rotate a image
-(void)rotate:(int)degree {
float rad = M_PI * (float)degree / 180.0;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
aktivImageView.transform = CGAffineTransformMakeRotation(rad);
[UIView commitAnimations];
}
i think you can also use this code snip with a button
in your IBAction you can code [self rotate:90]; to rate in 90° or you can use [self rotate:0]; to not rotate your image ;-)
精彩评论