~If you would like to request a tutorial, email Collateral at collateraldamag3@gmail.com~

Share This!

iPhone Tutorial ~ UIButtons and Animation

in




So you want to animate your UIButtons but don't know where to start? Well here is a guide that will have your UIButtons jumping around in no time. For this tutorial we will be using the "UIViewAnimationTransitionFlip" Animation. Basically, it flips stuff. 
If you look up most other tutorials on the web, they will say that in order to flip a button, you need 3 things: 2 buttons and a containing view. This is NOT TRUE. I will show you how to flip a single button. 
First, create your button, either in the Interface Builder or programmatically. If you don't know how to do that, go learn and come back. 
So, once you have your button (mine is named "btnMyButton") , make a method that gets called on UIControlEventTouchUpInside. 




- (IBAction) btn1_click
{

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5]; //Sets the Animation time to .5 Seconds

/*The Below Sets the transition to flip the button from the right. */
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:btnMyButton cache:YES];

[UIView commitAnimations];//Starts the Animation

}
That's it. Thats all you need to do to animate a button. Quite simple, really. The key that most people miss is that you can use a UIButton for the "forView:" key. Instead of assigning a UIView to this and flipping a button that is on top of the view, we are simply using the UIButton in the "forView:" key and flipping the button itself. 


~Collateral

4 comments:

Anonymous said...

I really liked this! :-) Elegant and simple.

G.P. said...

Great one Collateral... after 2hrs reading through non-working two buttons scenarios, I found this one. Excellent!

Anonymous said...

super-simple and works perfectly; many thanks

Anonymous said...

nice and awesome

Post a Comment

This entry is filed under .

You can also follow any responses to all entry through the RSS Comments feed.