首页 > 代码库 > 第三方库ATMHud的使用

第三方库ATMHud的使用

 

 

当网络中加载大量数据的时候,是不是给用户点提示比较好呢。这是一个例子,在AppDelegate中利用ATMHud里的代码,重新写了我们需要的函数。然后,在Utils中进行了二次封装,以使我们以后能够很简单的来使用这些函数。

 

上代码:

 

首先是整个文件布局的截图

 

 

 

需要我们先导入第三方库ATMHud,这个就不用说了。怎么导入,相信你一定知道的。

 

然后,依次代码。

 

AppDelegate.h

 

//加入头文件#import "ATMHud.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>{    ATMHud *myHud;}//网络好或坏的时候的提示框- (void) hudShow;- (void) hudShow : (NSString*) msg;- (void) hudSuccessHidden;- (void) hudSuccessHidden : (NSString*) msg;- (void) hudFailHidden;- (void) hudFailHidden : (NSString*) msg;

 

 

AppDelegate.m

 

#import "AppDelegate.h"//加入头文件#import "RootViewController.h"#import "Utils.h"@implementation AppDelegate#pragma -mark -网络连接不好,或错误时弹出的提示框- (void) hudShow{    NSLog(@"hudShow");    [self.window.rootViewController.view addSubview:myHud.view];    //显示的文字    [myHud setCaption:@"正在加载..."];    //加载的图片    [myHud setImage:nil];    //转圈    [myHud setActivity:YES];    //显示    [myHud show];}- (void) hudShow : (NSString*) msg{    NSLog(@"hudShow:msg");    [self.window.rootViewController.view addSubview:myHud.view];    [myHud setCaption:msg];    [myHud setImage:nil];    [myHud setActivity:YES];    [myHud show];    }- (void) hudSuccessHidden{    NSLog(@"hudSuccessHidden");        [myHud setCaption:@"加载完成"];    [myHud setActivity:NO];    [myHud setImage:[Utils getImageFromProject:@"19-check"]];    [self performSelector:@selector(setHudHidden) withObject:nil afterDelay:1.0f];}- (void) hudSuccessHidden : (NSString*) msg{    NSLog(@"hudSuccessHidden:msg");    [myHud setCaption:msg];    [myHud setActivity:NO];    [myHud setImage:[Utils getImageFromProject:@"19-check"]];    [myHud update];    [self performSelector:@selector(setHudHidden) withObject:nil afterDelay:1.0f];}- (void) hudFailHidden{    NSLog(@"hudFailHidden");    [myHud setCaption:@"当前网络不可用..."];    [myHud setActivity:NO];    [myHud setImage:[Utils getImageFromProject:@"11-x"]];    [myHud update];    [self performSelector:@selector(setHudHidden) withObject:nil afterDelay:1.0f];}- (void) hudFailHidden : (NSString*) msg{    NSLog(@"hudFailHidden:msg");    [myHud setCaption:msg];    [myHud setActivity:NO];    [myHud setImage:[Utils getImageFromProject:@"11-x"]];    [myHud update];    [self performSelector:@selector(setHudHidden) withObject:nil afterDelay:1.0f];}- (void) setHudHidden{    NSLog(@"setHudHidden");    [myHud hide];    [myHud.view removeFromSuperview];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.            //一定要初始化,否则是不会有效果的。    myHud = [[ATMHud alloc] init];        RootViewController *rootVC=[[RootViewController alloc]init];    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:rootVC];    self.window.rootViewController=nav;                self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}

 

Utils.h

 

#import <Foundation/Foundation.h>@interface Utils : NSObject//获取图片+ (UIImage *)getImageFromProject:(NSString *)path;+ (void) hudShow;+ (void) hudShow : (NSString*) msg;+ (void) hudSuccessHidden;+ (void) hudSuccessHidden : (NSString*) msg;+ (void) hudFailHidden;+ (void) hudFailHidden : (NSString*) msg;@end

 

 

Utils.m

 

#import "Utils.h"//加入头文件#import "AppDelegate.h"@implementation Utils//获取图片+ (UIImage *)getImageFromProject:(NSString *)path{    return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:path ofType:@"png"]];}//网络好或坏的时候的提示框+ (void) hudShow{    [(AppDelegate*)[[UIApplication sharedApplication] delegate] hudShow];}+ (void) hudShow : (NSString*) msg{    [(AppDelegate*)[[UIApplication sharedApplication] delegate] hudShow:msg];}+ (void) hudSuccessHidden{    [(AppDelegate*)[[UIApplication sharedApplication] delegate] hudSuccessHidden];}+ (void) hudSuccessHidden : (NSString*) msg{    [(AppDelegate*)[[UIApplication sharedApplication] delegate] hudSuccessHidden:msg];}+ (void) hudFailHidden{    [(AppDelegate*)[[UIApplication sharedApplication] delegate] hudFailHidden];}+ (void) hudFailHidden : (NSString*) msg{    [(AppDelegate*)[[UIApplication sharedApplication] delegate] hudFailHidden:msg];}@end

 

RootViewController.m

 

//加入头文件#import "Utils.h"- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.             [Utils hudShow];        [Utils hudShow:@"开始加载"];        [Utils hudSuccessHidden];        [Utils hudSuccessHidden:@"加载完成"];        [Utils hudFailHidden];        [Utils hudFailHidden:@"加载失败"];      }

 

 

好啦,测试成功!!!