UIProgressView 进度指示器
UIProgressView 与UIActivityIndicatorView 相似,只不过它提供了一个接口让你可以显示一个类似进度条的东西,这样就能让用户知道当前操作完成了多少(告知用户离操作结束还多远)。
一.创建
UIProgressView* progressView = [ [ UIProgressView alloc ]
initWithFrame:CGRectMake(150.0,20.0,130.0,30.0)];
二.设置风格:
progressView .UIProgressViewStyle= UIProgressViewStyleDefault;
系统给你提供了2种风格:
UIProgressViewStyleDefault 标准进度条
UIProgressViewStyleDefault 深灰色进度条,用于工具栏中
三.显示
[ self.toolBar addSubview:progressView ];
四.进度
当它显示出来时你的程序可以更新它的进度,属性progress是一个0.0到1.0之间的浮点数:
progressView.progress= 0. 5;
五.示例代码
4.09UIProgressView(1)
- (void)progressChanged:(NSTimer*)timer
{
if(_progressView.progress>=1.0) {
[timerinvalidate];
}
//如果对象的progress大于1.0,则定时器失效
_progressView.progress+=0.02;
//让对象的progress值每次都加0.02,以此达到让进度动起来的效果。
}
- (void)viewDidLoad
{
[superviewDidLoad];
self.progressView= [[UIProgressViewalloc]init];
//创建一个UIProgressView对象:_progressView
_progressView.frame=CGRectMake(0,20,320,0);
//设置它的位置及大小,它的高是默认的为9,可以写成0。
_progressView.progressViewStyle=UIProgressViewStyleDefault;
//设置它的风格,为默认的
_progressView.trackTintColor= [UIColorblueColor];
//设置轨道的颜色
_progressView.progressTintColor= [UIColorwhiteColor];
//设置进度的颜色
_progressView.progress=0.0;
//设置进度的初始值,即初始位置。范围是0.0-1.0
[self.viewaddSubview:_progressView];
//把_progressView加入到view上
[_progressViewrelease];
//要记得release
[NSTimerscheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(progressChanged:)
userInfo:nil
repeats:YES];
//设置定时器
}
第二种介绍
UIProgressView:
作用:
动态显示当前工作状态进度。
progressView
Progress bar view
1.h file
// Created by denny chen on 12-7-8.
// Copyright (c) 2012年__MyCompanyName__. All rights reserved.
//
#import
@interfaceProgressViewViewController :UIViewController
{
UIProgressView*progressview;
UIProgressView*barprogressview;
NSTimer*timer;
IBOutletUIButton*startButton;
IBOutletUIButton*stopButton;
}
@property(nonatomic,retain)IBOutletUIProgressView*progressview;
@property(nonatomic,retain)IBOutletUIProgressView*barprogressview;
-(IBAction) startProgress:(id)sender;
-(IBAction) stopProgress:(id)sender;
@end
2 .m file
@implementationProgressViewViewController
@synthesizeprogressview;
@synthesizebarprogressview;
- (void)viewDidLoad
{
self.progressview=nil;
self.barprogressview=nil;
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
stopButton.enabled=NO;
startButton.enabled=YES;
}
-(void) dealloc
{
self.progressview=nil;
self.barprogressview=nil;
[superdealloc];
}
- (void)viewDidUnload
{
self.progressview=nil;
self.barprogressview=nil;
[superviewDidUnload];
// Release any retained subviews of the main view.
}
/*进度条每次加0.01 */
-(void) timerChanged:(id)sender
{
self.progressview.progress+=0.01f;
self.barprogressview.progress+=0.01f;
}
-(IBAction) startProgress:(id)sender
{
timer=[NSTimerscheduledTimerWithTimeInterval:0.03f
target:self
selector:@selector(timerChanged:)
userInfo:nil
repeats:YES];
//[timerretain];
startButton.enabled=NO;
stopButton.enabled=YES;
}
-(IBAction) stopProgress:(id)sender
{
[timerinvalidate];
//[timerrelease];
//timer=nil;
self.progressview.progress=0.0f;
self.barprogressview.progress=0.0f;
startButton.enabled=YES;
stopButton.enabled=NO;
}
网友评论