UIProgressView doesn't update <=0.5 value a

2019-07-26 13:13发布

问题:

I want to represent a countdown timer. The progress view should change color for <70% - Yellow, <50% -red. But UIProgressView doesn't update after changing ProgressTintColor.(For 0 < x < 0.5).

But It works fine when I comment

[self.progressView setProgressTintColor: [UIColor redColor]]; //this line.

The below code will Trigger for each second:

float value = (float)secondsLeft / (float)secTotalDuration;
if (value < 0.9) {
      if (isChange == NO) {
           //[self.progressView setTranslatesAutoresizingMaskIntoConstraints: NO]; //- I tried THIS.

          [self.progressView setProgressTintColor: [UIColor redColor]]; //LINE - #1
          [self.progressView setTrackTintColor: [UIColor clearColor]];

          isChange = YES;
      }

      dispatch_async(dispatch_get_main_queue(), ^{ //With or without mainqueue doen't affect Uiprogressview change while commenting LINE - #1
                [self.progressView setProgress:value animated:NO];
      });

      NSLog(@"Progress: %f", [self.progressView progress]);

And LOG:

 Progress: 1.000000
 Progress: 0.800000
 Progress: 0.700000
 Progress: 0.500000 //Doesn't updated after this.
 Progress: 0.300000
 Progress: 0.200000

回答1:

Please try this :

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

       //Progress Value           
         [self.progressView setProgress:value animated:NO];

      //Progress Color        
         if (value < 0.5) {

           [self.progressView setProgressTintColor: [UIColor redColor]];  
           [self.progressView setTrackTintColor: [UIColor clearColor]];   
         }
         else if (value < 0.7) {

          [self.progressView setProgressTintColor: [UIColor yellowColor]];  
          [self.progressView setTrackTintColor: [UIColor clearColor]];
         }
         else if (value < 1.0) {       

          [self.progressView setProgressTintColor: [UIColor greenColor]];  
          [self.progressView setTrackTintColor: [UIColor clearColor]];

         }
}

Then Call it

float value = (float)secondsLeft / (float)secTotalDuration;

[self performSelectorOnMainThread:@selector(updateProgress:) withObject:@(value) waitUntilDone:NO];