Animation and Objective C
I’ve been playing around with some more advanced animation tools but wanted to go over the basics of how to animate objects.
First and foremost, you have to stop thinking about animation as a traditionally called function in that there are no {} or () notating where the animation starts. It’s simplified to the point where I wasn’t sure I was doing it right until I built my code and it worked!
Essentially you need to tell your code you’re about to animate with “[UIView beginAnimations:nil context:nil];”. You should also set up time parameters and how you want the animation to happen. Then, you simply say where you want your object to animate to (in my case a UIImageView containing a tennis ball). You can animate alpha, position, size, and – getting more advanced – you can animate openGL objects in 3D planes. After all this is done you tell your code to animate with “[UIView commitAnimations];”.
Here’s the basic code that I’m firing every time the user clicks a button that I’ve assigned this action to:
-(IBAction)moveBall:(id)sender{
if(ballUp){
NSLog(@”ball is up”);
//animate the ball down[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];CGRect offScreenFrame = theBall.frame;
offScreenFrame.origin.y = 20;
theBall.frame = offScreenFrame;[UIView commitAnimations];
ballUp = NO;
}else{
NSLog(@”ball is down”);
//animate the ball up[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];CGRect offScreenFrame = theBall.frame;
offScreenFrame.origin.y = 680;
theBall.frame = offScreenFrame;[UIView commitAnimations];
ballUp = YES;
}
}
If you’re having trouble watching the video, click here!
Download the zip file with all the code here.
Questions, comments, etc… Let me know!











