您的位置:首页 > 移动开发 > IOS开发

iOS MBProgressHUD 特效 加载界面

2014-07-28 09:23 176 查看


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

分类: 菜鸟学iOS的笔记2013-01-16
13:55 10711人阅读 评论(9) 收藏 举报

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

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

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

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





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

[cpp] view
plaincopy

- (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];

}];

}

[cpp] view
plaincopy

- (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];

}

}

} TE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: