in my app I have a UIProgressView that shows the time elapsed between two NSDate. Everything works properly, each minute that passes UIProgressView advances by 0.1 per minute.
The difference between the two dates is calculated in this way with a CGFloat
Besides that I have a UIButton it should have the function of "decrease the ProgressView 0.1" in a few words each time the button is pushed I would like to take one minute to the difference between the two NSDate that I created previously. I made a few attempts but I can not get this because when my app is closed and then reopened the time difference between the two dates do not change ... how can I do this?
I show you the code I'm using
-(void)viewDidLoad {
[super viewDidLoad];
[self startDate];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self upgradeEnergyBar];
[NSTimer scheduledTimerWithTimeInterval: (1 * 60)
target:self
selector:@selector(upgradeEnergyBar)
userInfo:nil
repeats:YES];
}
-(void)startDate {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:@"notFirstRun"]) {
NSDate *date = [NSDate new];
[[NSUserDefaults standardUserDefaults] setObject:date forKey:@"open"];
NSLog(@"Ora di apertura : %@", date);
[defaults setBool:YES forKey:@"notFirstRun"];
}
}
- (IBAction)sfAction:(id)sender {
//Decrement CGFLOAT
}
-(void)upgradeEnergyBar {
self.now = [NSDate new];
NSDate *lastDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"open"];
NSTimeInterval timeInterval = [self.now timeIntervalSinceDate:lastDate];
_minutes = timeInterval / 60;
_energyBar.progress = _minutes * 0.1;
[self updateLabelEnergyWithProgress:_energyBar.progress];
_minute.text = [NSString stringWithFormat:@"MINUTES:%ld", (long) _minutes];
NSLog(@"MINUTES:%ld", (long) _minutes);
if (_energyBar.progress == 1) NSLog(@"end");
}