状态栏显示进度加载

来源:互联网 发布:淘宝上传视频要求 编辑:程序博客网 时间:2024/05/22 14:06

子类化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 = [superinitWithFrame:frame];

    if (self) {

        //设置window的优先级

        self.windowLevel =UIWindowLevelStatusBar;

        //置背景颜色

        self.backgroundColor = [UIColorblackColor];

    }

    returnself;

}

//显示状态栏进度加载

- (void)showProgressView:(NSString *)text show:(BOOL)show operation:(AFHTTPRequestOperation *)operation

{

    

    //创建加载进度条

    //做安全判断,如果已经创建,则不再重复创建

    if (_progressView ==nil) {

        _progressView = [[UIProgressViewalloc] initWithFrame:CGRectMake(0,15, kScreenWidth,5)];

    }

    [selfaddSubview:_progressView];

    

    //创建sendLabel显示文字,正在加载,加载成功

    //做安全判断,如果已经创建,则不再重复创建

    if (_sendLabel ==nil) {

        _sendLabel = [[UILabelalloc] initWithFrame:CGRectMake(0,0, kScreenWidth,15)];

        //字体大小

        _sendLabel.font = [UIFontsystemFontOfSize:11];

        _sendLabel.backgroundColor = [UIColorclearColor];

        //居中对齐

        _sendLabel.textAlignment =NSTextAlignmentCenter;

        //设置字体颜色

        _sendLabel.textColor = [UIColorwhiteColor];

    }

    [selfaddSubview:_sendLabel];

    

    _sendLabel.text = text;

    //window设置为不隐藏

    self.hidden =NO;

    

    if(show) {

        //显示window

        //使用第三方框架AFNetworking中的"UIProgressView+AFNetworking"文件实现上传进度显示

        [_progressViewsetProgressWithUploadProgressOfOperation:operationanimated:YES];

        

    }else {

        //延迟2秒,隐藏window

        [selfperformSelector:@selector(hidden)withObject:nilafterDelay:2];

    }

}

//隐藏

- (void)hidden

{

    self.hidden =YES;

}

@end


在调用这个方法时,

- (void)showProgressView:(NSString *)text show:(BOOL)show operation:(AFHTTPRequestOperation *)operation;

先创建StatusBarWindow,

然后

[statusWindowshowProgressView:@"正在发送"show:YESoperation:operation];






0 0
原创粉丝点击