如何用uiview自定义进度条

 我来答
匿名用户
2016-08-04
展开全部
1、ProcessIndicatorView.h
#import <UIKit/UIKit.h>

@interface ProcessIndicatorView : UIView
{
UIActivityIndicatorView *indicator;
UILabel *textLabel;
}

-(void)do_startAnimating;
-(void)do_stopAnimating;
-(void)do_changeProcessInfo:(NSString *)infoText;
@end

2、ProcessIndicatorView.m
#import "ProcessIndicatorView.h"

@implementation ProcessIndicatorView

-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
float width = frame.size.width;
float height = frame.size.height;
indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
//设置显示样式,见UIActivityIndicatorViewStyle的定义
indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
//indicator.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);//设置显示位置
indicator.backgroundColor = [UIColor grayColor];
//设置背景透明
indicator.alpha = 0.5;
indicator.layer.cornerRadius = 5;
indicator.layer.masksToBounds = YES;
//将初始化好的indicator add到view中
[self addSubview:indicator];

textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, height-30, width, 30)];
[self addSubview:textLabel];
textLabel.textColor = [UIColor grayColor];
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.font = [UIFont systemFontOfSize:15];
}
return self;
}

-(void)do_startAnimating{
self.hidden = NO;
[indicator startAnimating];
textLabel.text = @"";
}
-(void)do_stopAnimating{
[indicator stopAnimating];
textLabel.text = @"";
self.hidden = YES;
}
-(void)do_changeProcessInfo:(NSString *)infoText{
textLabel.text = infoText;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/

@end

3、如何使用
3.1 初始化组件
float pos_x = (self.view.frame.size.width-100)/2.0;
float pos_y = (self.view.frame.size.height-100)/2.0;
processView = [[ProcessIndicatorView alloc] initWithFrame:CGRectMake(pos_x, pos_y, 100, 100)];

[self.view addSubview:processView];

3.2 调用方法
[processView do_startAnimating];
.........
[self saveImage:image withName:file_name];
NSLog(@"do_saveImageAndUploadJob is done... 照片的文件名 >>>> file_name =%@ ",file_name);
[processView do_stopAnimating];

3.3 配合ASIFormDataRequest
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: url]];
.........
[request setUploadProgressDelegate:self];
request.showAccurateProgress=YES;

3.4 实现对应的代理方法
//显示最新的上传发送进度
-(void)setProgress:(float)newProgress{
NSString *progressText = [NSString stringWithFormat:@"%0.f%%",newProgress*100];
[processView do_changeProcessInfo:progressText];
}

//-(void)request:(ASIHTTPRequest *)request didSendBytes:(long long)bytes{
// NSLog(@">>>> didSendBytes: %lld", bytes);
//}
//-(void)request:(ASIHTTPRequest *)request incrementUploadSizeBy:(long long)newLength{
// NSLog(@"incrementUploadSizeBy:%lld",newLength);
//}
/* 二者区别在于第2个参数的不同,前者的bytes参数是每次发送的字节数(不累加),后者的newLength参数是每次发送时已发送的字节书(累加)。 需要注意的是这个参数很大,为longlong类型,转换为字符串时可以用%lld格式化字符串: */
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式