2012-08-23 27 views

回答

2

你可以使用一个简单的NSTimer来实现这一目标:

当然,在viewDidLoad,这些变量都需要在你的头文件中声明。

UIProgressView *myProgressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; 
float someFloat = 0; 

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(methodToUpdateProgress) userInfo:nil repeats:YES]; 

那么这将更新进度视图(假设最小/最大值是0-100)

- (void)methodToUpdateProgress 
{ 
    if(someFloat == 100){ 
     [myTimer invalidate]; 
    }else{ 
     someFloat = someFloat + 12; 
     [myProgressView setProgress:someFloat animated:YES]; 
    } 
} 

此外,如果在此被称为时间实际上是一个关注这个例子中应该可以帮助你很多。 引述:How do I use NSTimer?

NSDate *d = [NSDate dateWithTimeIntervalSinceNow: 60.0]; 
NSTimer *t = [[NSTimer alloc] initWithFireDate: d 
           interval: 1 
           target: self 
           selector:@selector(onTick:) 
           userInfo:nil repeats:YES]; 

NSRunLoop *runner = [NSRunLoop currentRunLoop]; 
[runner addTimer:t forMode: NSDefaultRunLoopMode]; 
[t release]; 

注:这是一个非常粗略的例子,但它应该得到整个点。希望这可以帮助!

0

的ivars:

NSDate *_startDate = .... 
NSDate *_finishDate = .... 
UIProgressBarView *_progressBar = .... 

triggerMethod:

- (void)start 
{ 
    [self updateProgressBar]; 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:updateInterval 
                target:self 
                selector:@selector(updateProgressBar) 
                userInfo:nil 
                repeats:YES]; 
} 

更新progressbarview

- (void)updateProgressBar 
{ 
    NSTimeInterval diff = [_finishDate timeintervalSince:_startDate]; 
    NSTimeInterval pastTime = [_finishDate timeIntervallSinceNow]; 
    [_progressBar setProgress:pastTime/diff animated:YES]; 

} 

不要忘记无效,当它完成计时器和dealloc方法。

如果在代码的其他地方保存完成并开始日期。然后,即使解除分配,您也可以使用相同的状态重新创建视图。这意味着用户不需要打开该视图1小时。例如。他/她在30分钟后关闭并打开。