【问题标题】:How to set progress in UIProgressView如何在 UIProgressView 中设置进度
【发布时间】:2015-01-19 09:32:12
【问题描述】:

我在计算 UIProgressView 的进度时遇到问题。我的浮点值对进度没有影响。我尝试手动设置进度,它工作正常,但如果我尝试计算它就不起作用。

这是我的代码:

- (void) initProgressbar {

   self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
   [self.progressView setFrame:CGRectMake(0, 0, SCREEN_WIDTH / 2, 10)];
   self.progressView.center = CGPointMake(SCREEN_WIDTH - 110, SCREEN_HEIGHT - 25);
   self.progressView.progress = 0.0;
   [self.view addSubview:self.progressView];
}

nbElementsSyncnbElementsToSync 是全局 int 属性,nbElementsSync 在调用 updateProgress 方法之前通过循环递增。

MyController.h

@interface MyController : UIViewController {

   NSString *json;
   int nbElementsToSync;
   int nbElementsSync;

}

@property (nonatomic, strong) UIProgressView *progressView;

MyController.m

nbElementsSync = 0; // Nb elements synchronized
nbElementsToSync = [[json valueForKey:@"count"] intValue]; // Nb elements to synchronize

for (NSString* result in results) {

    nbElementsSync++;
    [self updateProgress];

}

这是我设置进度的方法:

- (void) updateProgress {

   [self.progressView setProgress:((float)nbElementsSync / nbElementsToSync)];
   NSLog(@"percent : %f", ((float)nbElementsSync / nbElementsToSync));

}

我的 NSLog 的结果:

percent : 0.003937
percent : 0.007874
percent : 0.011811
percent : 0.015748
percent : 0.019685
percent : 0.023622
...

有什么办法解决吗?提前致谢。

【问题讨论】:

  • 你能展示你在循环中实现的代码吗?nbElementsToSync 是否随时间变化?
  • 那么你从中得到了什么?确保您的数组“结果”计数与您的“nbElementsToSync”相同
  • 你确定你没有在另一个线程上调用 updateProgress 吗?您只能从主线程更新 UI。尝试记录它,如果它为零,那么这是一个问题 NSLog(@"%@", @([NSThread isMainThread]))
  • NSLog(@"%@", @([NSThread isMainThread])) 总是返回 1

标签: ios objective-c progress uiprogressview


【解决方案1】:

您是否尝试过在后台进程中执行循环,而不是阻止 UI 更新:

MyController.m

在你需要的地方调用它:

[self performSelectorInBackground:@selector(syncInBackground) withObject:nil];

然后

- (void)syncInBackground
{
    int nbElementsSync = 0; // Nb elements synchronized
    int nbElementsToSync = [[json valueForKey:@"count"] intValue]; // Nb elements to synchronize

    for (NSString* result in results) {
        nbElementsSync++;

        float percent = (float)nbElementsSync / nbElementsToSync;

        [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:percent] waitUntilDone:NO];
    }   
}

- (void) updateProgress:(NSNumber *)percent {

    [self.progressView setProgress:percent.floatValue];
}

【讨论】:

  • 不,我没有。我的同步必须在主线程中而不是在后台。我不明白为什么当我可以 NSLog 百分比并且返回的值在 0.0 和 1.0 之间时,我的进度在 UIProgressView 上不可见......
  • 当您在主线程(例如循环)上运行繁重的进程时,您会阻塞 UI 并且它将无法更新。您应该在后台线程上执行循环并在主线程上更新 progressView
  • OK Niko,但我的循环是在多个 NSURL 连接的结果中执行的,因此将其实现为后台线程会很复杂......
  • 如果根据您的代码我没看错,您需要 2 个元素来计算百分比:结果和 nbElementsToSync。第一个想法:创建一个包含您需要的所有元素的对象,然后将其提供给 withObject 参数 [self performSelectorInBackground:@selector(syncInBackground) withObject:myObjectWithElements];如果结果或 nbElementsToSync 值发生变化,您必须找到在后台同步的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2017-12-21
  • 2011-04-18
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多