Skip to content

Instantly share code, notes, and snippets.

@dbreunig
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbreunig/9333708 to your computer and use it in GitHub Desktop.
Save dbreunig/9333708 to your computer and use it in GitHub Desktop.
A method which returns a time interval, adjusted for time since initial method call. The caller specifies the initial time (the speed at which an animation might run during first run) and an eventual time (which the time interval will decay towards). Set the kTotalDecayTime to the number of days it will take for the time interval to fully decay.…
#define kInitialAnimationDate @"InitialAnimationDate"
#define kTotalDecayTime 14 // Number of days for initialTime to decay to eventual time
+ (NSTimeInterval)intervalStartingFrom:(NSTimeInterval)initialTime decayingTowards:(NSTimeInterval)eventualTime
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDate *creationDate = [defaults objectForKey:kInitialAnimationDate];
NSUInteger currentAge;
// Create the date if it hasn't been saved yet
if ( !creationDate ) {
[defaults setObject:[NSDate date] forKey:kInitialAnimationDate];
[defaults synchronize];
currentAge = 0;
} else {
// Set the current age to the number of days since first launch
currentAge = [creationDate timeIntervalSinceNow] / 60 / 24;
}
// Determine the % of the decay time which has elapsed
double days = kTotalDecayTime - currentAge;
days = days < 0 ? 0 : days;
// Return the adjusted time
return eventualTime + ( (initialTime - eventualTime) * (days / kTotalDecayTime ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment