前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >ios学习(七)MBProgressHUD特效

ios学习(七)MBProgressHUD特效

作者头像
Java架构师必看
发布2021-05-31 15:34:36
发布2021-05-31 15:34:36
58400
代码可运行
举报
文章被收录于专栏:Java架构师必看Java架构师必看
运行总次数:0
代码可运行

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

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

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

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

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

代码语言:javascript
代码运行次数:0
复制
- (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];  
    }];  
}  
代码语言:javascript
代码运行次数:0
复制
- (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的显示信息

代码语言:javascript
代码运行次数:0
复制
+ (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进行通知“错误 网络无连接”;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档