#import "ViewController.h"
@interface ViewController ()
{
UIProgressView* progressView1;
UIProgressView* progressView2;
UIProgressView* progressView3;
UIButton *button1;
UIButton *button2;
UIButton *button3;
}
@end
@implementation ViewController
//定义定时器
NSTimer *timer;
CGFloat progval;//定义变量,纪录当前的进度值
- (void)viewDidLoad {
[super viewDidLoad];
progressView1= [[UIProgressView alloc]initWithFrame:CGRectMake(50.0,40.0,200.0,30.0)];
progressView2= [[UIProgressView alloc]initWithFrame:CGRectMake(50.0,120.0,200.0,30.0)];
progressView1.progressViewStyle= UIProgressViewStyleDefault;
progressView2.progressViewStyle= UIProgressViewStyleBar; //设置进度条风格
[self.view addSubview:progressView1];
[self.view addSubview:progressView2];
button1=[[UIButton alloc]initWithFrame:CGRectMake(50, 400, 100, 50)];
[button1 setTitle:@"开始" forState:UIControlStateNormal];
button1.backgroundColor=[UIColor orangeColor];
[button1 addTarget:self action:@selector(aa:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
button2=[[UIButton alloc]initWithFrame:CGRectMake(180, 400, 100, 50)];
[button2 setTitle:@"停止" forState:UIControlStateNormal];
button2.backgroundColor=[UIColor purpleColor];
[button2 addTarget:self action:@selector(bb:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
button3=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 50)];
[button3 setTitle:@"暂停" forState:UIControlStateNormal];
button3.backgroundColor=[UIColor greenColor];
[button3 addTarget:self action:@selector(cc:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)aa:(id)sender
{
progval=0;
timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changeProgress) userInfo:nil repeats:YES];
button1.enabled = NO;
button2.enabled = YES;
}
-(IBAction)bb:(id)sender
{
[timer invalidate];//停用记时器
progressView1.progress=0.0f;
progressView2.progress=0.0f;
button1.enabled = YES;
button2.enabled = NO;
}
-(IBAction)cc:(id)sender
{
[timer invalidate];
button3=[[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 50)];
[button3 setTitle:@"重新开始" forState:UIControlStateNormal];
button3.backgroundColor=[UIColor greenColor];
[button3 addTarget:self action:@selector(dd:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
}
-(IBAction)dd:(id)sender
{
[button3 setHidden:YES];
timer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(changeProgress) userInfo:nil repeats:YES];
}
-(void)changeProgress
{
progval+=0.01;
if (progval>=1.0) {
[timer invalidate];
}
else{
//同时改变进度条的进度值
[progressView1 setProgress:progval animated:YES];
[progressView2 setProgress:progval animated:YES];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end