NSTimer を使用して UIProgressView を更新し、1 秒ごとに進行状況が 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];
}
したがって、理想的には、foo メソッドの実行中に UIProgressView の進行状況が増加します。ただし、タイマーは foo メソッドが終了した後に開始されるため、コンソールは次のようになります...
START Foo
FINISH Foo
Increase by 0.1
Increase by 0.1
Increase by 0.1
And so on ....
なぜこれが起こるのでしょうか?さらに重要なことは、foo が呼び出される前に、どうすればやりたいことを実行し、タイマーの実行を開始できるかということです。