即使在主线程中 运行 时,UIProgressView 进度也不会更新
UIProgressView progress not updating even when run in the main thread
UIProgressView 进度未在 iOS8&9 中更新,但在 iOS7 中工作,即使 运行 在主线程中也是如此。我的代码如下,希望对你有帮助。
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target:self selector: @selector(handleProgressBar) userInfo: nil repeats: YES];
}
- (void) handleProgressBar{
if(self.usedTime >= 300.0)
{
[self.timer invalidate];
self.timer=nil;
[self submitAns:[NSNumber numberWithInt:-1]];
}
else
{
self.usedTime += 1;
CGFloat progress = self.usedTime*(0.0033333333);
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
if(self.usedTime>200){
[self.progressBar setProgressTintColor:[UIColor redColor]];}
}
}
- (void)updateProgress:(NSNumber *)progress {
float fprogress = [progress floatValue];
//self.progressBar.progress = fprogress;
[self.progressBar setProgress:fprogress animated:YES];
}
@property (strong, nonatomic) UIProgressView *progressView;
@property (nonatomic) CGFloat usedTime;
我把它放在viewDidLoad
方法中
_progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 500, self.view.bounds.size.width, 50)];
[self.progressView setProgress:0.0];
[self.view addSubview:self.progressView];
self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target:self selector: @selector(handleProgressBar) userInfo: nil repeats: YES];
[![enter image description here][1]][1]//-----------------------------------------------------------------------
- (void) handleProgressBar{
if(self.usedTime >= 300.0)
{
[self.timer invalidate];
self.timer=nil;
// [self submitAns:[NSNumber numberWithInt:-1]];
}
else
{
self.usedTime += 1;
CGFloat progress = self.usedTime*(0.0033333333);
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
if(self.usedTime>200){
[self.progressView setProgressTintColor:[UIColor redColor]];}
}
}
//-----------------------------------------------------------------------
- (void)updateProgress:(NSNumber *)progress {
float fprogress = [progress floatValue];
//self.progressBar.progress = fprogress;
[self.progressView setProgress:fprogress animated:YES];
}
//-----------------------------------------------------------------------
UIProgressView 进度未在 iOS8&9 中更新,但在 iOS7 中工作,即使 运行 在主线程中也是如此。我的代码如下,希望对你有帮助。
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target:self selector: @selector(handleProgressBar) userInfo: nil repeats: YES];
}
- (void) handleProgressBar{
if(self.usedTime >= 300.0)
{
[self.timer invalidate];
self.timer=nil;
[self submitAns:[NSNumber numberWithInt:-1]];
}
else
{
self.usedTime += 1;
CGFloat progress = self.usedTime*(0.0033333333);
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
if(self.usedTime>200){
[self.progressBar setProgressTintColor:[UIColor redColor]];}
}
}
- (void)updateProgress:(NSNumber *)progress {
float fprogress = [progress floatValue];
//self.progressBar.progress = fprogress;
[self.progressBar setProgress:fprogress animated:YES];
}
@property (strong, nonatomic) UIProgressView *progressView;
@property (nonatomic) CGFloat usedTime;
我把它放在viewDidLoad
方法中
_progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 500, self.view.bounds.size.width, 50)];
[self.progressView setProgress:0.0];
[self.view addSubview:self.progressView];
self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target:self selector: @selector(handleProgressBar) userInfo: nil repeats: YES];
[![enter image description here][1]][1]//-----------------------------------------------------------------------
- (void) handleProgressBar{
if(self.usedTime >= 300.0)
{
[self.timer invalidate];
self.timer=nil;
// [self submitAns:[NSNumber numberWithInt:-1]];
}
else
{
self.usedTime += 1;
CGFloat progress = self.usedTime*(0.0033333333);
[self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
if(self.usedTime>200){
[self.progressView setProgressTintColor:[UIColor redColor]];}
}
}
//-----------------------------------------------------------------------
- (void)updateProgress:(NSNumber *)progress {
float fprogress = [progress floatValue];
//self.progressBar.progress = fprogress;
[self.progressView setProgress:fprogress animated:YES];
}
//-----------------------------------------------------------------------