Sunday, January 30, 2011

Simple animation with iPhone SDK

Recently I was trying port my application on iPhone and want to convert my Qt Animation code to iPhone SDK.

I achieved animation using core animation toolkit provided by iPhone SDK. In iPhone animation is performed using UIView object. You can animate changes to view property like frame, bounds, alpha, center, etc....

On iOS 4 and later animation can be done using block objects but if you want to run your code on iOS 3.2 or earlier than you have to use older begin/commit method.

I used begin/commit method to perform animation in my code.

Following snippet shows sample code that I used to animate a button's center to certain location.

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];

// Make the animatable changes.
btn1.center = CGPointMake(150, btn1.center.y);

// Commit the changes and perform the animation.
[UIView commitAnimations];

Same code can also be written using new block object, like following.

[UIView animateWithDuration:1.0 animations:^{
btn1.center = CGPointMake(150, btn1.center.y);
}];

No comments:

Post a Comment