一、UIProgressView介紹
你使用uiprogressview類來描述任務發費時間的進度。一個進度條的例子是在下載郵件信息時,顯示在郵件應用程序的底部。uiprogressview類提供了用於管理進度條的樣式和用於獲取和設置在任務完成的進度值的屬性。
二、相關屬性和方法
@property(nonatomic) float progress 當前的進度,取值範圍爲0-1,1代表任務已經完成。
- (void)setProgress:(float)progress animated:(BOOL)animated 調整當前的進度
@property(nonatomic) UIProgressViewStyle progressViewStyle 可以設置當前進度條的類型
@property(nonatomic, strong) UIColor *progressTintColor ;可以用來設置已填充進度條的顏色
@property(nonatomic, strong) UIImage *progressImage;用來設置已填充進度條的圖像。如果設置這個值progressTintColor被忽略了
@property(nonatomic, strong) UIColor *trackTintColor;用來設置未填充進度條的顏色
@property(nonatomic, strong) UIImage *trackImage;用來設置未填充進度條的顏色。同樣的則trackTintColor被忽略了。
typedef enum {
UIProgressViewStyleDefault,
UIProgressViewStyleBar,
} UIProgressViewStyle;
枚舉代表進度條允許的類型。@interface AnotherViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>{
double proValue;
NSTimer *timer;
UIProgressView *progressView;
}
@end
-(void)initProgressView{
progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(10, 220, self.view.bounds.size.width-20, 40)];
progressView.progress = 0.0;//設置進度條起步
progressView.progressTintColor = [UIColor redColor];//設置已經填充進度條顏色
progressView.trackTintColor = [UIColor greenColor];//設置未填充進度條顏色
[self.view addSubview:progressView];
[progressView setProgressViewStyle:UIProgressViewStyleBar];//設置進度條類型
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 300, self.view.bounds.size.width-40, 44);
btn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(beginProgressButton:)
forControlEvents:UIControlEventTouchUpInside];
}
-(void)beginProgressButton:(id)sender{
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(progressOperator) userInfo:nil repeats:YES];
}
-(void)progressOperator{
proValue++;
if (proValue>10) {
[timer invalidate];
}else{
[progressView setProgress:proValue/10.0];
}
}
}