In March I created a splashView class which makes it easy to create a splash screen in your iPhone program. Since then, it's achieved a lot of attention,causing me to put together a slightly revised version of the class, along with some hopefully more straightforward instructions on how to use the class.
They follow, below.
The Code
You can download the new splashView class here: Download SplashView 1.1.
I've also put together a sample project with the splashView already in place. You can find that here: Download NavSample. It builds the splashView into the navigation template. That was chosen pretty arbitrarily; you could put the splashView into any other template, as you see fit.
Below is what you need to know about the class. Most of this, I've copied straight from my earlier article, but updated to correctly describe the usage for the current version of splashView. I'll talk about the changes a bit later.
The Methods
There are just two methods intended for external use.
initWithImage:
- (id)initWithImage:(UIImage *)screenImage;
Give the SplashView a 320x460 image that will be used to fill the screen.
startSplash
- (void)startSplash;
Once you've set all your properties, run this to start the timer of the SplashView going.
The Properties
I filled SplashView with properties, so that you can use it in whatever
manner you see fit. Here's a run-down of all of the ones intended to be
user accessible:
animation (enum). What sort of animation
to show when the SplashView disappears. Options are
SplashViewAnimationNone, SplashViewAnimationSlideLeft, and
SplashViewAnimationFade. Default is SplashViewAnimationNone (no
animation).
animationDelay (NSTimeInterval). How long the animation should run. Default is 1s.
delay (NSTimeInterval). How long the SplashView displays before it disappears. Default is 2s.
delegate (id). What object will listen for splashView's optional delegate protocol.
touchAllowed (BOOL). Whether a user can touch the splash screen to immediately make it disappear (or start animating away). Default is NO.
The Protocol
Sometimes you'll want to know when your splashView closes down. That's what the protocol allows. You just need to set your responding object as the splashView's delegate and have it respond to the splashViewDelegate protocol. This will give you access to one protocol method:
splashIsDone
- (void)splashIsDone;
If you want a reminder on how protocols are used, you should consult iPhone in Action p.179. If you want a reminder on how new protocols are created, you should read Create Delegate Protocols for the iPhone, an earlier article.
Using the Class
If you want to use this class in your program, you can do so under a simple Creative Commons license, which basically says that you'll leave our credits in the comments of the class. Other than that, it's free for your use.
Here's the steps to get it running:
- Copy the splashView.h and splashView.m files into your program using "Add > Existing Files". When you do so, be sure to check the box that says to physically copy them.
- Add the QuartzCore framework to your project.
- Decide which file you're going to activate the splashView from. I suggest the app delegate as the most obvious location for this sort of big-picture content, but I think the splashView is now set up correctly so that you can run it from anywhere.
- #import "splashView.h" into the appropriate file.
- Create your 320x460 PNG. I suggest naming it Default.png. Copy it into your program using "Add > Existing Files", again being sure to check the box so that it gets physically copied.
- Code the splashView startup.
Coding the splashView startup just requires the use of the init and startSplash methods previously noted. Here's a minimalist example:
splashView *mySplash = [[splashView alloc] initWithImage:
[UIImage imageNamed:@"Default.png"]];
[mySplash startSplash];
[mySplash release];
For a less minimalist example, you'd set some of the properties before running startSplash. If anything isn't clear about how to put these pieces together, you should take a look at the navSample class, linked above.
About Default.png: So why did I suggest using Default.png for the filename? It's because the iPhone will automatically show a file by that name while a program is being loaded. It doesn't do any of the fancy transitions that this class does, nor does it offer any guarantees about how long it'll stay up, but other than that it could be used as a low-budget splash screen (though the purpose was actually to imply that the program was loading faster than it really was).
If you name your image Default.png, then you get the best of both worlds. The PNG will show while the program loads, then the splashView will simultaneously start with your same image, which will eventually transition out via whatever animation method you defined. If you do this, you probably want to tune down the delay once you test it out on a real iPhone, so that users don't feel like they're looking at your splash screen for tool long.
The Changes
I'm going to talk a little bit about the code here, and you might want to first read part two of my original article series, which covers the header file and part three, which covers the source code file. Not only will this give you better context for this update, but it'll also show you the techniques that were used to create the splashView, building on the information from iPhone in Action.
There were two notable changes in this version of splashView.
The first thing I did was improve how the splashView got displayed. In the original version of the program, you had to add the splashView by hand as a subview of the main window. It restricted where you could run splashView, and it also required you to do the right thing or the class wouldn't work.
You'll notice that now you don't add the splashView as a subview at all(!). Somehow, the splashView is magically added to the main window.
It's actually down in the startSplash method, right at the start:
[[[[UIApplication sharedApplication] windows] objectAtIndex:0] addSubview:self];
This takes advantage of the fact that UIApplication has a listing of all the windows, and automatically places splashView atop the first one.
The second thing I did was add the protocol. This methodology is all covered in the creating delegate protocols article I already referenced. After setting up the protocol, I referenced it from dismissSplashFinish:
if (self.delegate != NULL
&& [self.delegate respondsToSelector:@selector(splashIsDone)]) {
[delegate splashIsDone];
}
As before, if you have questions, comments, or suggestions for improvements, please include them in the comments.
I can't at this time guarantee that the splashView works in iPhone OS 3.0, as I'm currently doing live development on 2.2.1.
Table Tricks & Tips, Part Four: Editing Tables
The answers, it turns out, is a very simple one:
That's literally all you need to do to set up those deletion marks. Then you just need to respond to tableView:commitEditingStyle: forRowAtIndexPath:.
However, there are some nuances, particularly the questions of how you start up a table's editing and how you end it, and I'm going to show some real-world examples of those methods over the course of this article ...
An Actual Example
The program that I've showed off elsewhere in this series makes uses of a tableView's editing functionality by allowing the user to delete characters from the app, each of whom are represented by a table row
First, you need to set up some way to activate the functionality:
This method is triggered when a user selects an "Edit" button. You'll note that besides starting the editing, I also change around the button in my navbar. That's because when a user is editing I no longer need an "Edit" button, but instead require a "Done" button. You might alternatively set buttons' enabled properties to NO ... but in any case, you always need to give users a way to get out of editing mode.
Your tableView: method will probably delete the table item from your table and/or the data store that it originates from:
For completeness state, I'll also include the method call generated when the "Done" button is clicked:
This is pretty much the opposite of my editCharacters method: the editing is turned back off, then the buttons are returned to their original state.
And that's really all you need to do to edit a table: turn editing on, respond to the deletion message, and turn it back off at the end.
Doing More with Table Edits
You do have some other editing possibilities that I'm not going to cover in depth in this article. Most notably, you can adjust the editingStyle property of any individual cell. Apple claims it's set to UITableViewCellEditingStyleNone by default, but it sure looks to me like it's set to UITableViewCellEditingStyleDelete, which is what shows those handsome red minus marks. If you instead want to insert rows, you can set it to UITableViewCellEditingStyleInsert, and then do the appropriate thing when tableView:commitEditingStyle:forRowAtIndexPath: receives input of type insert.
That's it for me and tables for the nonce. If there's any other topics that you'd like covered regarding them, let me know in the comments. In the meantime, I'm planning to next return to my splashView topic, as some folks have requested in comments.
Posted by Shannon Appelcline at 12:31 PM in iPhone in Action Commentary | Permalink | Comments (0) | TrackBack (0)