资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

开源中国iOS客户端学习——(七)MBProgressHUD特效-创新互联

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

创新互联于2013年创立,先为太仆寺等服务建站,太仆寺等地企业,进行企业商务咨询服务。为太仆寺企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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

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

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

开源中国iOS客户端学习——(七)MBProgressHUD特效  开源中国iOS客户端学习——(七)MBProgressHUD特效

我们在跳转到实现的代码部分,在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:@"错误 %@",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];                          //如果有网络 则缓存它             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的显示信息

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


另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:开源中国iOS客户端学习——(七)MBProgressHUD特效-创新互联
网页地址:http://cdkjz.cn/article/cdjohj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220