在开源中国iOS客户端中也用到了MBProgressHUD这个特效,主要作用为应用显示一个过渡的作用,常用于打开一个联网页面加载过程,防止出现假死现象,如果网速慢则告诉用户已经在很努力很努力的加载中。
GitHub上下载地址:https://github.com/jdg/MBProgressHUD
源码中也自带了一个Demo,显示13中动画效果,可以根据需要选取其中特效加以使用,使用方法基本一样;使用的时候只加把MBProgressHUD.h和MBProgressHUD.m拖入工程中,在使用的文件中加上#import"MBProgressHUD.h"
在开源中国iOS客户端中只用到一种特效,当我们选取一条资讯查看详细信息时:
我们在跳转到实现的代码部分,在NewsDetail.m的clickFavorite和viewDidLoad方法中
[cpp] view plaincopy
- - (void)clickFavorite:(id)sender
- {
- UIBarButtonItem * btn = (UIBarButtonItem *)sender;
- BOOL isFav = [btn.title isEqualToString:@"收藏此文"];
- MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
- [Tool showHUD:isFav ? @"正在添加收藏":@"正在删除收藏" andView:self.view andHUD:hud];
- [[AFOSCClient sharedClient]getPath:isFav ? api_favorite_add : api_favorite_delete
- parameters:[NSDictionary dictionaryWithObjectsAndKeys:
- [NSString stringWithFormat:@"%d", [Config Instance].getUID],@"uid",
- [NSString stringWithFormat:@"%d", newsID],@"objid",
- @"4",@"type", nil] success:^(AFHTTPRequestOperation *operation, id responseObject) {
- [hud hide:YES];
- [Tool getOSCNotice2:operation.responseString];
- ApiError *error = [Tool getApiError2:operation.responseString];
- if (error == nil) {
- [Tool ToastNotification:operation.responseString andView:self.view andLoading:NO andIsBottom:NO];
- return ;
- }
- switch (error.errorCode)
- {
- case 1:
- {
- btnFavorite.title = isFav ? @"取消收藏" : @"收藏此文";
- self.singleNews.favorite = !self.singleNews.favorite;
- }
- break;
- case 0:
- case -2:
- case -1:
- {
- [Tool ToastNotification:[NSString stringWithFormat:@"错误 %@",error.errorMessage] andView:self.view andLoading:NO andIsBottom:NO];
- }
- break;
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- [hud hide:YES];
- [Tool ToastNotification:@"添加收藏失败" andView:self.view andLoading:NO andIsBottom:NO];
- }];
- }
[cpp] view plaincopy
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.tabBarItem.title = @"资讯详情";
- self.tabBarItem.image = [UIImage imageNamed:@"detail"];
- //WebView的背景颜色去除
- [Tool clearWebViewBackground:self.webView];
- self.singleNews = [[SingleNews alloc] init];
- self.navigationController.title = @"资讯详情";
- self.webView.delegate = self;
- [self.webView loadHTMLString:@"" baseURL:nil];
- if ([Config Instance].isNetworkRunning)
- {
- MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
- [Tool showHUD:@"正在加载" andView:self.view andHUD:hud];
- NSString *url = [NSString stringWithFormat:@"%@?id=%d",api_news_detail, newsID];
- [[AFOSCClient sharedClient] getPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
- [Tool getOSCNotice2:operation.responseString];
- [hud hide:YES];
- self.singleNews = [Tool readStrNewsDetail:operation.responseString];
- if (self.singleNews == nil) {
- [Tool ToastNotification:@"加载失败" andView:self.view andLoading:NO andIsBottom:NO];
- return;
- }
- [self loadData:self.singleNews];
- //如果有网络 则缓存它
- if ([Config Instance].isNetworkRunning)
- {
- [Tool saveCache:1 andID:self.singleNews._id andString:operation.responseString];
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- [hud hide:YES];
- if ([Config Instance].isNetworkRunning) {
- [Tool ToastNotification:@"错误 网络无连接" andView:self.view andLoading:NO andIsBottom:NO];
- }
- }];
- }
- else
- {
- NSString *value = [Tool getCache:1 andID:newsID];
- if (value) {
- self.singleNews = [Tool readStrNewsDetail:value];
- [self loadData:self.singleNews];
- }
- else {
- [Tool ToastNotification:@"错误 网络无连接" andView:self.view andLoading:NO andIsBottom:NO];
- }
- }
- }
分析viewDidLoad方法,
首先是判断网络是否连通状态,如果是
定义在当前的view中定义一个MBProgressHUD对象,进行初始化
[ToolshowHUD:@"正在加载" andView:self.viewandHUD:hud];是在Tool类里面进行的一次封装,设置MBProgressHUD的显示信息
[cpp] view plaincopy
- + (void)showHUD:(NSString *)text andView:(UIView *)view andHUD:(MBProgressHUD *)hud
- {
- [view addSubview:hud];
- hud.labelText = text;//显示提示
- hud.dimBackground = YES;//使背景成黑灰色,让MBProgressHUD成高亮显示
- hud.square = YES;//设置显示框的高度和宽度一样
- [hud show:YES];
- }
然后在用到AFNetWork类库的getPath:parameters:success:failure:方法,嵌套在block块中判断请求的url是否成功,在执行[Tool getOSCNotice2:operation.responseString];这个方法也是封装在Tool类中,封装的是TBXML解析器,如果解析成功立即设置MBProgressHUD隐藏属性[hud hide:YES];如果请求的url不成功直接设置MBProgressHUD隐藏属性[hud hide:YES],再用GCDiscreetNotificationView进行通知“错误 网络无连接”;