2014-11-03 75 views
0

我正在處理項目時,我已經放入了一個進度條,然後設置了一個按鈕以在按下時啓動進度條。事情是,我想在按下startbutton的30分鐘內取得進展,所以我需要幫助將其設置爲30分鐘。使用UIProgressView將進度條設置爲30分鐘

這裏是我的代碼來設置進度,並startbutton

M文件

- (IBAction)StartButton:(id)sender { 
    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moreProgress) 
    userInfo:Nil repeats:YES]; 
    [super viewDidLoad]; 
    //progressbar start when button pressed 
    } 

,然後我得到這個的時候

//selector progress time 
- (void) moreProgress { 
    myProgressView.progress += 0.001; 
} 
+0

我不知道我理解你的問題。你的意思是你想讓progressView花費30分鐘才能變滿,對嗎? – 2014-11-03 14:58:13

+0

是:)多數民衆贊成在我想要的 – Andy 2014-11-03 15:06:18

回答

1

我想這只是一個數學問題。

首先,myProgressView.progress可以使用從0.0到1.0的值。

當你每秒打電話給moreProgress時,你需要在30分鐘內打電話1800次。因此,myProgressView.progress應該每秒增加1/1800。

你的代碼更改爲:

- (void) moreProgress { 
    myProgressView.progress += 1.0/1800.0; 
} 

希望它可以幫助你! :)

+0

它幫助你嗎?如果是這樣,你能否接受它作爲正確答案?謝謝! :) @andy – 2014-11-03 19:28:21

+0

是的。謝謝!:) – Andy 2014-11-04 06:51:09

+0

如果我有一個停止按鈕,我如何停止計時器,與invalidate方法? – Andy 2014-11-06 08:12:28

1

1)保存當前時間(您可以通過示例將其保存到NSUserDefaults)。 你可以做這樣的事情:

NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults]; 

NSDate *currentDate = [NSDate date]; 

[user1 setObject:[NSNumber numberWithDouble:[currentDate timeIntervalSince1970]] forKey:@"time"]; 
[user1 synchronize]; 

2)安排調用你的函數像你這樣做是

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moreProgress) 
    userInfo:Nil repeats:YES]; 

3)接下來在你的工作,你需要更新的東西進度視圖狀態這樣的:

//selector progress time 
- (void) moreProgress { 
NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; 
NSDate *startTime = [NSDate dateWithTimeIntervalSince1970:[[user objectForKey:@"time"]doubleValue]]; 
NSTimeInterval elapsedTime = [startTime timeIntervalSinceNow]; 
float progress = (float)elapsed time/30.0 * 60.0; 
myProgressView.progress = progress; 

if ((float)elapsedTime/30 * 60 >= 1) 
{ 
    // The timer is end, you should stop it and do what you need 
}  
} 

注: 這不是安全的期望

timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(moreProgress) 

將每秒啓動。它不能保證也不準確。