objective-c - 在 objective-c 中使用基于时间的 ProgressBar

标签 objective-c ios progress-bar nstimer

嗨,我想在我的 iPhone 应用程序中使用基于时间的进度条,例如 如果一个人在上午 10:00 开始旅程并在上午 11:00 结束,那么我将每 5 分钟更新一次与当前时间相比的进度, 怎么可能

最佳答案

您可以使用一个简单的 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];

然后这将更新进度 View (假设最小/最大值为 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];

注意:这是一个非常粗略的例子,但它应该能说明问题。希望这对您有所帮助!

关于objective-c - 在 objective-c 中使用基于时间的 ProgressBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087125/

相关文章:

ios - audioPlayer.isPlaying 语句上的线程 1 错误

objective-c - 从 shell 脚本读取实时输出并更新进度条

javascript - 带有持续时间和百分比的进度条

Android 动画旋转

iphone - Objective C 中是否有与 Android 的 aidl 文件等效的文件?

ios - 旋转 UISplitViewController 后模态消失

ios - 从tableview单元格更新文本并刷新后关闭键盘

iphone - 在大图像分辨率上执行时出现 EXC_BAD_ACCESS 错误

ios - 如何从后台任务添加和重绘 CALayer?

ios - 解码 CLLocationAccuracy 常量