0

I have a countdown that counts down from 5. I use KVO on a float property that represents the countdown. It fires observeValueForKeyPath once or not at all. I want it fire observeValueForKeyPath until the property being observed matches a particular value, so it can fire a method in observeValueForKeyPath.

Please see code

EDIT

 __block float progress;
    self.countdown.labelVCBlock = ^(KAProgressLabel *label) {
        progress = 5-(label.progress *5);
        label.text = [NSString stringWithFormat:@"%.f", (progress)];


    };

     self.progressFloat = 5.0f; //setting the initial value
     [self addObserver:self forKeyPath:@"progressFloat" options:NSKeyValueObservingOptionNew context:NULL];
    self.progressFloat -= progress;//progress is a float that counts down from 5

}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

    if ([keyPath isEqualToString:@"progressFloat"]) {
          NSLog(@"KVO called");
        if (self.progressFloat < 0.1f) {//when self.progressFloat is less than 0.1 second, then fire the method below. 

            [self takePhoto:nil];
        }
        }
    }

The method either fires immediately or observeValueForKeyPath never gets called.

noobsmcgoobs
  • 2,716
  • 5
  • 32
  • 52

1 Answers1

0

I see that you don't update your variable progressFloat in block. Try change code to:

__unsafe_unretained typeof(self) weakSelf = self;
self.countdown.labelVCBlock = ^(KAProgressLabel *label) {
    progress = 5-(label.progress *5);
    label.text = [NSString stringWithFormat:@"%.f", (progress)];
    weakSelf.progressFloat -= progress;
};
vien vu
  • 4,277
  • 2
  • 17
  • 30