I am trying to make an application where, when you launch the app and image starts to rotate automatically after launch. For some reason the way I am doing it, it needs a button or something to rotate, but I want it to rotate automatically. Maybe I have to take it out of the IBAction.. Or I don't know. I am pretty new to programming. Any help would be AWESOME!
It's simple code. The .h sets up the image and - (IBAction)rotate; The .m then does:
Code:
- (IBAction)rotate {
// starts the animation
[UIView beginAnimations:nil context:NULL];
// sets the time before animation occurs
[UIView setAnimationDelay:0];
// sets the length of the animation
[UIView setAnimationDuration:2.0];
[UIView setAnimationRepeatCount:3]; //spin it 3 times.
// 3.14 is pi. -3.14 would be counter clockwise
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14/2);
//square is the UIImageView getting rotated
square.transform = CGAffineTransformRotate(transform, 3.14/2);
// ends animation
[UIView commitAnimations];
}
Now here is the problem. I did exactly what you said and put the animation code inside of the - (void)viewDidLoad and the spin animation starts automatically like I wanted it to. However, every time the image gets back to 0˚, it slows down and then speeds back up... Is there anyway I can get it to smoothly rotate? Thanks!
Now here is the problem. I did exactly what you said and put the animation code inside of the - (void)viewDidLoad and the spin animation starts automatically like I wanted it to. However, every time the image gets back to 0˚, it slows down and then speeds back up... Is there anyway I can get it to smoothly rotate? Thanks!
Animations use ease-in-ease-out timing by default. Add the following line between your beginAnimations and commitAnimations:
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
It technically doesn't matter but I meant make the method you had void instead of IBAction (also void but if you aren't using it in Interface Builder take it out) and call the method in viewDidLoad.
Thanks so much. I did exactly what you said and it worked but... When the image rotates, it goes 360˚ but when it gets to around 357˚ it skips. It is not a fully smooth transition. Any suggestions?
Code:
- (void)viewDidLoad
{
[super viewDidLoad];
// starts the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
// sets the time before animation occurs
// sets the length of the animation
[UIView setAnimationDuration:4.0];
[UIView setAnimationRepeatCount:1e100f]; //spin it forever.
// 3.14 is pi. -3.14 would be counter clockwise
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14/2);
//bluegoal is the UIImageView getting rotated
bluegoal.transform = CGAffineTransformRotate(transform, 3.14/2);
// ends animation
[UIView commitAnimations];
// Do any additional setup after loading the view from its nib.
}
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.