我使用 NSTimer 来更新 UIProgressView 以便每秒进度增加 0.1。
-(void)updateBar{
NSLog(@"Increase by 0.1");
pbar.progress=pbar.progress+0.1;
}
-(void)foo{
NSLog("START Foo");
//Does some stuff that takes about 10sec
NSLog("FINISH Foo");
}
-(void) viewDidLoad{
pbar = [[UIProgressView alloc] initWithFrame:CGRectMake(-280, 500, 730, 90)];
pbar.progressViewStyle = UIProgressViewStyleDefault;
pbar.progressTintColor = [UIColor colorWithRed:68.0/255 green:186.0/255 blue:41.0/255 alpha:1.0];
pbar.progress = 0.0;
pbar.transform = CGAffineTransformMakeRotation(90 * M_PI / 180.0);
[self.view addSubview:pbar];
[NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(updateBar) userInfo:nil repeats: YES];
[self foo];
}
所以理想情况下,UIProgressView 的进度会在 foo 方法运行期间增加。但是,计时器在 foo 方法完成后启动,所以我的控制台最终看起来像这样......
START Foo
FINISH Foo
Increase by 0.1
Increase by 0.1
Increase by 0.1
And so on ....
知道为什么会发生这种情况吗?更重要的是,我如何让它做我想做的事情并让计时器在 foo 被调用之前开始运行。