您好,登錄后才能下訂單哦!
在開(kāi)源中國(guó)iOS客戶端中也用到了MBProgressHUD這個(gè)特效,主要作用為應(yīng)用顯示一個(gè)過(guò)渡的作用,常用于打開(kāi)一個(gè)聯(lián)網(wǎng)頁(yè)面加載過(guò)程,防止出現(xiàn)假死現(xiàn)象,如果網(wǎng)速慢則告訴用戶已經(jīng)在很努力很努力的加載中。
GitHub上下載地址:https://github.com/jdg/MBProgressHUD
源碼中也自帶了一個(gè)Demo,顯示13中動(dòng)畫(huà)效果,可以根據(jù)需要選取其中特效加以使用,使用方法基本一樣;使用的時(shí)候只加把MBProgressHUD.h和MBProgressHUD.m拖入工程中,在使用的文件中加上#import"MBProgressHUD.h"
在開(kāi)源中國(guó)iOS客戶端中只用到一種特效,當(dāng)我們選取一條資訊查看詳細(xì)信息時(shí):
我們?cè)谔D(zhuǎn)到實(shí)現(xiàn)的代碼部分,在NewsDetail.m的clickFavorite和viewDidLoad方法中
- (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:@"錯(cuò)誤 %@",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]; }]; }
- (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]; //如果有網(wǎng)絡(luò) 則緩存它 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:@"錯(cuò)誤 網(wǎng)絡(luò)無(wú)連接" 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:@"錯(cuò)誤 網(wǎng)絡(luò)無(wú)連接" andView:self.view andLoading:NO andIsBottom:NO]; } } }
首先是判斷網(wǎng)絡(luò)是否連通狀態(tài),如果是
定義在當(dāng)前的view中定義一個(gè)MBProgressHUD對(duì)象,進(jìn)行初始化
[ToolshowHUD:@"正在加載" andView:self.viewandHUD:hud];是在Tool類里面進(jìn)行的一次封裝,設(shè)置MBProgressHUD的顯示信息
+ (void)showHUD:(NSString *)text andView:(UIView *)view andHUD:(MBProgressHUD *)hud { [view addSubview:hud]; hud.labelText = text;//顯示提示 hud.dimBackground = YES;//使背景成黑灰色,讓MBProgressHUD成高亮顯示 hud.square = YES;//設(shè)置顯示框的高度和寬度一樣 [hud show:YES]; }然后在用到AFNetWork類庫(kù)的getPath:parameters:success:failure:方法,嵌套在block塊中判斷請(qǐng)求的url是否成功,在執(zhí)行[Tool getOSCNotice2:operation.responseString];這個(gè)方法也是封裝在Tool類中,封裝的是TBXML解析器,如果解析成功立即設(shè)置MBProgressHUD隱藏屬性[hud hide:YES];如果請(qǐng)求的url不成功直接設(shè)置MBProgressHUD隱藏屬性[hud hide:YES],再用GCDiscreetNotificationView進(jìn)行通知“錯(cuò)誤 網(wǎng)絡(luò)無(wú)連接”;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。