開源中國iOS客戶端學習——(七)MBProgressHUD特效

在開源中國iOS客戶端中也用到了MBProgressHUD這個特效,主要做用爲應用顯示一個過渡的做用,經常使用於打開一個聯網頁面加載過程,防止出現假死現象,若是網速慢則告訴用戶已經在很努力很努力的加載中。 git

GitHub上下載地址:https://github.com/jdg/MBProgressHUD github

源碼中也自帶了一個Demo,顯示13中動畫效果,能夠根據須要選取其中特效加以使用,使用方法基本同樣;使用的時候只加把MBProgressHUD.h和MBProgressHUD.m拖入工程中,在使用的文件中加上#import"MBProgressHUD.h" web


在開源中國iOS客戶端中只用到一種特效,當咱們選取一條資訊查看詳細信息時: api

  

咱們在跳轉到實現的代碼部分,在NewsDetail.m的clickFavorite和viewDidLoad方法中 緩存

  1. - (void)clickFavorite:(id)sender  
  2. {  
  3.     UIBarButtonItem * btn = (UIBarButtonItem *)sender;  
  4.     BOOL isFav = [btn.title isEqualToString:@"收藏此文"];  
  5.   
  6.     MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];  
  7.     [Tool showHUD:isFav ? @"正在添加收藏":@"正在刪除收藏" andView:self.view andHUD:hud];  
  8.     [[AFOSCClient sharedClient]getPath:isFav ? api_favorite_add : api_favorite_delete   
  9.                             parameters:[NSDictionary dictionaryWithObjectsAndKeys:  
  10.                                         [NSString stringWithFormat:@"%d", [Config Instance].getUID],@"uid",  
  11.                                         [NSString stringWithFormat:@"%d", newsID],@"objid",  
  12.                                         @"4",@"type", nil] success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  13.                                   
  14.                                 [hud hide:YES];  
  15.                                 [Tool getOSCNotice2:operation.responseString];  
  16.                             
  17.                                 ApiError *error = [Tool getApiError2:operation.responseString];  
  18.                                 if (error == nil) {  
  19.                                     [Tool ToastNotification:operation.responseString andView:self.view andLoading:NO andIsBottom:NO];  
  20.                                     return ;  
  21.                                 }  
  22.                                 switch (error.errorCode)   
  23.                                 {  
  24.                                     case 1:  
  25.                                     {  
  26.                                         btnFavorite.title = isFav ? @"取消收藏" : @"收藏此文";  
  27.                                         self.singleNews.favorite = !self.singleNews.favorite;  
  28.                                     }  
  29.                                         break;  
  30.                                     case 0:  
  31.                                     case -2:  
  32.                                     case -1:  
  33.                                     {  
  34.                                         [Tool ToastNotification:[NSString stringWithFormat:@"錯誤 %@",error.errorMessage] andView:self.view andLoading:NO andIsBottom:NO];  
  35.                                     }  
  36.                                         break;  
  37.                                 }  
  38.   
  39.           
  40.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  41.         [hud hide:YES];  
  42.         [Tool ToastNotification:@"添加收藏失敗" andView:self.view andLoading:NO andIsBottom:NO];  
  43.     }];  
  44. }  

  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     self.tabBarItem.title = @"資訊詳情";  
  5.     self.tabBarItem.image = [UIImage imageNamed:@"detail"];  
  6.     //WebView的背景顏色去除  
  7.     [Tool clearWebViewBackground:self.webView];  
  8.       
  9.     self.singleNews = [[SingleNews alloc] init];  
  10.     self.navigationController.title = @"資訊詳情";  
  11.     self.webView.delegate = self;  
  12.     [self.webView loadHTMLString:@"" baseURL:nil];  
  13.       
  14.     if ([Config Instance].isNetworkRunning)   
  15.     {  
  16.         MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];  
  17.         [Tool showHUD:@"正在加載" andView:self.view andHUD:hud];  
  18.           
  19.         NSString *url = [NSString stringWithFormat:@"%@?id=%d",api_news_detail, newsID];  
  20.         [[AFOSCClient sharedClient] getPath:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  21.               
  22.             [Tool getOSCNotice2:operation.responseString];  
  23.             [hud hide:YES];  
  24.               
  25.             self.singleNews = [Tool readStrNewsDetail:operation.responseString];  
  26.             if (self.singleNews == nil) {  
  27.                 [Tool ToastNotification:@"加載失敗" andView:self.view andLoading:NO andIsBottom:NO];  
  28.                 return;  
  29.             }  
  30.             [self loadData:self.singleNews];  
  31.               
  32.             //若是有網絡 則緩存它  
  33.             if ([Config Instance].isNetworkRunning)   
  34.             {  
  35.                 [Tool saveCache:1 andID:self.singleNews._id andString:operation.responseString];  
  36.             }  
  37.               
  38.         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  39.               
  40.             [hud hide:YES];  
  41.             if ([Config Instance].isNetworkRunning) {  
  42.                 [Tool ToastNotification:@"錯誤 網絡無鏈接" andView:self.view andLoading:NO andIsBottom:NO];  
  43.             }  
  44.               
  45.         }];  
  46.     }  
  47.     else  
  48.     {  
  49.         NSString *value = [Tool getCache:1 andID:newsID];  
  50.         if (value) {  
  51.             self.singleNews = [Tool readStrNewsDetail:value];  
  52.             [self loadData:self.singleNews];  
  53.         }  
  54.         else {  
  55.             [Tool ToastNotification:@"錯誤 網絡無鏈接" andView:self.view andLoading:NO andIsBottom:NO];  
  56.         }  
  57.     }  
  58. }  

分析viewDidLoad方法,

首先是判斷網絡是否連通狀態,若是是 網絡

定義在當前的view中定義一個MBProgressHUD對象,進行初始化 ide

[ToolshowHUD:@"正在加載" andView:self.viewandHUD:hud];是在Tool類裏面進行的一次封裝,設置MBProgressHUD的顯示信息 動畫

  1. + (void)showHUD:(NSString *)text andView:(UIView *)view andHUD:(MBProgressHUD *)hud  
  2. {  
  3.     [view addSubview:hud];  
  4.     hud.labelText = text;//顯示提示  
  5.     hud.dimBackground = YES;//使背景成黑灰色,讓MBProgressHUD成高亮顯示  
  6.     hud.square = YES;//設置顯示框的高度和寬度同樣  
  7.     [hud show:YES];  
  8. }  
而後在用到AFNetWork類庫的getPath:parameters:success:failure:方法,嵌套在block塊中判斷請求的url是否成功,在執行[Tool getOSCNotice2:operation.responseString];這個方法也是封裝在Tool類中,封裝的是TBXML解析器,若是解析成功當即設置MBProgressHUD隱藏屬性[hud hide:YES];若是請求的url不成功直接設置MBProgressHUD隱藏屬性[hud hide:YES],再用GCDiscreetNotificationView進行通知「錯誤 網絡無鏈接」;
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息