您的位置:首页 > 其它

状态栏显示进度加载

2015-09-22 10:03 411 查看
子类化UIWindow,在状态栏上创建UIProgressView加载进度条,创建UILabel显示文字
//StatusBarWindow.h
@interface StatusBarWindow :
UIWindow
{
UIProgressView *_progressView;
UILabel *_sendLabel;
}
//显示状态栏进度加载
- (void)showProgressView:(NSString *)text show:(BOOL)show operation:(AFHTTPRequestOperation
*)operation;
//隐藏
- (void)hidden;

#import "StatusBarWindow.h"
#import "UIProgressView+AFNetworking.h"
@implementation StatusBarWindow
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
//设置window的优先级
self.windowLevel =
UIWindowLevelStatusBar;
//设置背景颜色
self.backgroundColor = [UIColor
blackColor];
}
return
self;
}
//显示状态栏进度加载
- (void)showProgressView:(NSString *)text show:(BOOL)show operation:(AFHTTPRequestOperation
*)operation
{

//创建加载进度条
//做安全判断,如果已经创建,则不再重复创建
if (_progressView ==
nil) {
_progressView = [[UIProgressView
alloc] initWithFrame:CGRectMake(0,
15, kScreenWidth,
5)];
}
[self
addSubview:_progressView];

//创建sendLabel显示文字,正在加载,加载成功
//做安全判断,如果已经创建,则不再重复创建
if (_sendLabel ==
nil) {
_sendLabel = [[UILabel
alloc] initWithFrame:CGRectMake(0,
0, kScreenWidth,
15)];
//字体大小
_sendLabel.font = [UIFont
systemFontOfSize:11];
_sendLabel.backgroundColor = [UIColor
clearColor];
//居中对齐
_sendLabel.textAlignment =
NSTextAlignmentCenter;
//设置字体颜色
_sendLabel.textColor = [UIColor
whiteColor];
}
[self
addSubview:_sendLabel];

_sendLabel.text = text;
//window设置为不隐藏
self.hidden =
NO;

if(show) {
//显示window
//使用第三方框架AFNetworking中的"UIProgressView+AFNetworking"文件实现上传进度显示
[_progressView
setProgressWithUploadProgressOfOperation:operation
animated:YES];

}else {
//延迟2秒,隐藏window
[self
performSelector:@selector(hidden)
withObject:nil
afterDelay:2];
}
}
//隐藏
- (void)hidden
{
self.hidden =
YES;
}
@end

在调用这个方法时,
- (void)showProgressView:(NSString *)text show:(BOOL)show operation:(AFHTTPRequestOperation *)operation;
先创建StatusBarWindow,
然后
[statusWindow
showProgressView:@"正在发送"
show:YES
operation:operation];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: