Last active
August 29, 2015 13:56
-
-
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.…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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