简体   繁体   English

在objective-c中使用基于Time的ProgressBar

[英]Using ProgressBar based on Time in objective-c

嗨,我想在我的iPhone应用程序中根据时间使用进度条,如果一个人开始旅程从上午10:00开始并在11:00结束然后每5分钟我将更新进度与当前时间相比,它是怎么回事可能

You could use a simple NSTimer to achieve this: 您可以使用简单的NSTimer来实现此目的:

In viewDidLoad of course, these variables will need to be declared in your header file. viewDidLoad ,当然需要在头文件中声明这些变量。

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

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

Then this will update the progress view (assuming min/max value is 0-100) 然后这将更新进度视图(假设最小/最大值为0-100)

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

Additionally, if the time at which this is called is actually a concern this example should help you a lot. 此外,如果调用它的时间实际上是一个问题,这个例子应该会帮助你很多。 Quoted From: How do I use NSTimer? 引自: 我如何使用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];

NOTE: This is a pretty rough example, but it should get the point across. 注意:这是一个非常粗略的例子,但它应该得到重点。 Hope this helps! 希望这可以帮助!

iVars: 高德:

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

triggerMethod: triggerMethod:

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

update progressbarview 更新进度条视图

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

}

Do not forget to invalidate timer when it is finished and in dealloc method. 在完成和dealloc方法时,不要忘记使计时器无效。

If you save your finish and start date somewhere else in your code. 如果您保存完成并在代码中的其他位置开始日期。 Then you can recreate the view with same state even it is deallocated. 然后,即使已取消分配,也可以使用相同的状态重新创建视图。 It means the user do not need have open that view 1 hour. 这意味着用户不需要打开该视图1小时。 eg. 例如。 he/she close and open after 30 min. 他/她在30分钟后关闭并打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM