UIProgressView顧名思義用來顯示進度的,如音樂,視頻的播放進度,和文件的上傳下載進度等。
下面以一個簡單的實例來介紹UIprogressView的使用。
@interface ActivityViewController : UIViewController
{
UIProgressView *proView;
double proValue;
NSTimer *timer;
}
@property(nonatomic, retain) UIProgressView *proView;
-(IBAction)btnStartClick;
@implementation ActivityViewController
@synthesize proView;
#pragma mark - View lifecycle
-(IBAction)btnStartClick
{
proValue=0;
timer = [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(changeProgress) userInfo:nilrepeats:YES]; //利用計時器,每隔1秒調用一次(changeProgress)
}
-(void)changeProgress
{
proValue += 1.0; //改變proValue的值
if(proValue > 5)
{
//停用計時器
[timer invalidate];
}
else
{
[proViewsetProgress:(proValue / 5)];//重置進度條
}
}
- (void)viewDidLoad
{
proView = [[UIProgressViewalloc] initWithFrame:CGRectMake(100, 100, 150, 20)];
[proViewsetProgressViewStyle:UIProgressViewStyleDefault]; //設置進度條類型
[self.viewaddSubview:proView];
[superviewDidLoad];
}