自定义UIView简单实现进度条

参考网上的资料,自己实现了一个上传进度条的组件

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格式化字符串: */

猜你喜欢

转载自zhengjj-2009.iteye.com/blog/2191392