This time i will show you how to have a custom HUD that show in the center of the screen while you are loading some files or receiving data from internet.
This HUD most of the times is required by Apple, cause they
reject apps that don't alert the final user that the device seems "freezed" while loading.
So i suggest you use it if your app must wait some seconds doing something.
1. Link Library
Go to the project build window and link libraries.
Press add libraries and chose:
- UIKit.framework
- QuartzCore.framework
If when you add the library it goes out of your Framework group (folder) drag it into the one you want and after drag again it from the folder to the link libraries window (this is a trick to do faster).
2. Importing Libraries
Those libraries are usually common when you start a project, i suggest to import in your UntitledViewController.h:
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
3. Adding the functions
First we add the declaration in the UntitledViewController.h
//HUD
- (void) ShowHUD:(NSString*)title :(NSString*)subtitle;
- (void) CloseHUD;
4. Adding the real code
Add this to UntitledViewController.m
//*****************************************************
// HUD
//*****************************************************
- (void) ShowHUD:(NSString*)title :(NSString*)subtitle
{
HUD = [[UIView alloc] initWithFrame:CGRectMake((DEVICE_WIDTH / 2) - 150, (DEVICE_HEIGHT / 2) - 60, 300, 120)];
HUD.tag = 999;
HUD.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.6];
//HUD.clipsToBounds = YES;
//HUD.center = appDelegate.viewController.view.center;
HUD.layer.cornerRadius = 5.0;
HUD.layer.borderWidth = 1.0;
HUD.layer.borderColor = [UIColor lightGrayColor].CGColor;
hud_indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(135, 40, 35, 35)];
hud_indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
//hud_indicator.color = [UIColor whiteColor];
hud_indicator.clipsToBounds = YES;
hud_indicator.hidesWhenStopped = YES;
//Titolo
UILabel *_titolo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 280, 20)];
_titolo.backgroundColor = [UIColor clearColor];
_titolo.textColor = [UIColor whiteColor];
_titolo.textAlignment = UITextAlignmentCenter;
_titolo.font = [UIFont systemFontOfSize:14];
_titolo.text = title;
_titolo.frame = CGRectIntegral(_titolo.frame);
//Sottotitolo
UILabel *_subtitolo = [[UILabel alloc] initWithFrame:CGRectMake(10, 75, 280, 40)];
_subtitolo.backgroundColor = [UIColor clearColor];
_subtitolo.textColor = [UIColor whiteColor];
_subtitolo.textAlignment = UITextAlignmentCenter;
_subtitolo.lineBreakMode = UILineBreakModeWordWrap;
_subtitolo.numberOfLines = 0;
_subtitolo.font = [UIFont systemFontOfSize:12];
_subtitolo.text = subtitle;
_subtitolo.frame = CGRectIntegral(_subtitolo.frame);
[HUD addSubview:hud_indicator];
[HUD addSubview:_titolo];
[HUD addSubview:_subtitolo];
[appDelegate.viewController.view addSubview:HUD];
[appDelegate.viewController.view bringSubviewToFront:HUD];
[hud_indicator startAnimating];
[_titolo release];
[_subtitolo release];
}
- (void) CloseHUD
{
[hud_indicator stopAnimating];
[self DeleteView:999];
hud_indicator = nil;
[hud_indicator release];
HUD = nil;
[HUD release];
}
In this code you can find some italian words:
- titolo = title
- sottotitolo/subtitolo = subtitle
So change it if you prefer another language
5. Show the HUD
Now if you remember how we shared the UntitledViewController in AppGameKit in the c++ file, you know we can do, if you don't remember it go to the end of the post :
[VC showHUD :@"Now Loading..." :"This is the subtitle info..."];
6. Delete the HUD when you are done
Bonus function to delete views
You need this function to have HUD work.
UntitledViewController.h
- (void) DeleteView:(NSInteger) taggo;
UntitledViewController.m
- (void) DeleteView:(NSInteger) taggo
{
appDelegate = (iphone_appAppDelegate *) [[UIApplication sharedApplication] delegate];
UIView *temp = [appDelegate.viewController.view viewWithTag:taggo];
if([temp isFirstResponder]){
[temp resignFirstResponder];
[temp endEditing:TRUE];
}
[temp removeFromSuperview];
}
Just to remember it....
Trick to have the UntitledViewController in the C++ AGK
Add this line to your declaration in the AppGameKit template.c;
UntitledViewController *VC = [UntitledViewController alloc];
Hope you enjoy this and ask question if you need!
Long life to Steve!