博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MBProgressHUD特效(菊花第三方)
阅读量:7191 次
发布时间:2019-06-29

本文共 4331 字,大约阅读时间需要 14 分钟。

考虑到还会有很多童鞋会看到该Blog,在此做一个说明,开源中国iOS开源客户端源码已做重构,请下载最新的源码学习。

新repo地址:。

另外,这篇Blog收集了一些其他社区的客户端源码,源码也正在不断更新中,有的也上架appstore。

访问地址:

---------------------------------------------------------------------------------------------------------------------------

在开源中国iOS客户端中也用到了MBProgressHUD这个特效,主要作用为应用显示一个过渡的作用,常用于打开一个联网页面加载过程,防止出现假死现象,如果网速慢则告诉用户已经在很努力很努力的加载中。

GitHub上下载地址:

源码中也自带了一个Demo,显示13中动画效果,可以根据需要选取其中特效加以使用,使用方法基本一样;使用的时候只加把MBProgressHUD.h和MBProgressHUD.m拖入工程中,在使用的文件中加上#import"MBProgressHUD.h"

在开源中国iOS客户端中只用到一种特效,当我们选取一条资讯查看详细信息时:

   

我们在跳转到实现的代码部分,在NewsDetail.m的clickFavorite和viewDidLoad方法中

[cpp] 
  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. }  


[cpp] 
  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对象,进行初始化

[ToolshowHUD:@"正在加载" andView:self.viewandHUD:hud];是在Tool类里面进行的一次封装,设置MBProgressHUD的显示信息

[cpp] 
  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进行通知“错误 网络无连接”;

转载于:https://www.cnblogs.com/yuhaojishuboke/p/5043097.html

你可能感兴趣的文章
利用ExpandableListView和gridview 显示可展开折叠菜单导航
查看>>
再看tp
查看>>
SQL Server 2012 还原选项的变化
查看>>
细节之处方显linux真功夫
查看>>
谈谈SQL Server高可用的常见问题
查看>>
Provisioning Services 7.8 入门系列教程之六 手动添加设备
查看>>
技术大牛对程序员招聘的吐槽和建议
查看>>
《未来架构师》的教学范例(2)
查看>>
Exchange 混合部署—Exchange 2007到Exchange 2013迁移
查看>>
C++构造函数和析构函数的学习(一)
查看>>
Redhat更新yum源
查看>>
jmeter企业内训简报
查看>>
你知道测试大牛怎么写测试计划的吗?
查看>>
ios程序在ios5下出现黑屏的问题
查看>>
运维APP番外篇
查看>>
Linux文件系统ext3与ext4主要区别手记
查看>>
系统集中运维管理平台【社区版】
查看>>
利用二层端口安全防止两个三层交换机长距离光纤线路被乱接测试
查看>>
《深度实践KVM》目录、前言、及前3章
查看>>
Windows Docker的有趣事实
查看>>