Coding under the Hammer
Archive for July, 2009
Simple UIView based Animations on the iPhone
Jul 18th
Although for complex animation sequences on the iPhone you need to use either the OpenGL or Apple’s very own Core Animation Framework, a lot of simple animations can be achieved with the methods found in the UIView class. All these animations are actually built upon Core Animation, but they have been wrapped up for you to use with very little code.
All animations triggered by the UIView class happen within a animation block.
To start an animation block you use the UIView Class method:
+ (void)beginAnimations:(NSString *)animationID context:(void *)context;
The animation ID is used in delegate call backs for such things as the beginning and end of animation blocks. The context is an additional piece of information that can be passed through.
As both of these parameters are optional, you comenly see:
[UIView beginAnimations:@"" context:NULL];
To commit these animations, and therefore end the animation block, you need to use the UIView class method:
+ (void)commitAnimations
So what types of animations can you do ? well quite a few.
You can animate views by:
- Changing their alpha
- Changing there size
- Changing there location
In a single animation block you can animate multiple views, and you are also able to nest animation blocks.
So lets do a common example. When a UITableViewCell is being edited you often want to make a UILabel’s (label) alpha change to 0 so it is hidden:
[UIView beginAnimations:@"" context:NULL];
[label setAlpha:(editing ? 0.0 : 1.0)];
[UIView commitAnimations];
For those new to C programming:
(editing ? 0.0 : 1.0)
Is equivlant to:
if(editing)
{
0.0;
}else{
1.0;
}
So for the second example we also have a UILabel (label), that we want to grow when the animation code is run. We also want this animation to last 2 seconds, so the user can admire our work. In addition to this, we want the animation to “ease in”. This is known as the animation curve. The animation curves that are available are
- ease in (slow then fast)
- ease out (fast then slow)
- ease in and out (slow, fast, slow)
- linear (constant speed)
We would do this animation using the following animation block:
[UIView beginAnimations:@"" context:NULL];
//The new frame size
[label setFrame: CGRectMake(0,0,320,100)];
//The animation duration
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelay: UIViewAnimationCurveEaseIn];
[UIView commitAnimations];
This is just a brief overview of the animations you can do using the UIView class, but it should be enough to get you started. The UIView methods also include delegate call backs for when an animation starts and end. For more information see Apple’s Documentation.
If the UIView class does not have what you need, you will probably need to use the Core Animation framework. While using this framework is not trivial, it is not as hard as using OpenGL (which is used commonly for 3D games), and you can build some fantastic animations using it.
Changing the name of an iPhone application
Jul 4th
When you finally come to release your application after months of hard work, your might want to change the name of the application that appears on the iPhone springboard, and if you use it, the settings application.
To change the name of an iPhone application is very easy. Simply open the Xcode project, and then scroll down to the “Targets” section, which the the “Groups & Files” part of Xcode. Select the application’s target and “Get info” on it using cmd-i or ctrl clicking it, and select “Get Info” from the menu.
In the build section of the get info window, you need to change the “Product Name” value to whatever you want your application to be called. When you change this value make sure that the configuration pop up menu (at the top of the window) is set to “All Configurations”, so it takes effect in all of your builds.
For the change of name to take effect, simply clean your targets and rebuild your application.