Skip to content

Commit

Permalink
Merge pull request #19 from nemesis/runloops
Browse files Browse the repository at this point in the history
Adding the timer to UITrackingRunLoop, also trimmed whitespace
  • Loading branch information
dataxpress committed Jul 23, 2014
2 parents 37246bd + f65b724 commit c4dd64c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions UICountingLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ -(void)countFrom:(float)startValue to:(float)endValue withDuration:(NSTimeInterv
[self runCompletionBlock];
return;
}

self.easingRate = 3.0f;
self.startingValue = startValue;
self.destinationValue = endValue;
self.progress = 0;
self.totalTime = duration;
self.lastUpdate = [NSDate timeIntervalSinceReferenceDate];

if(self.format == nil)
self.format = @"%f";

Expand All @@ -139,11 +139,12 @@ -(void)countFrom:(float)startValue to:(float)endValue withDuration:(NSTimeInterv
self.counter = [[UILabelCounterEaseInOut alloc] init];
break;
}

self.counter.rate = 3.0f;

NSTimer* timer = [NSTimer timerWithTimeInterval:(1.0f/30.0f) target:self selector:@selector(updateValue:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
}

-(void)updateValue:(NSTimer*)timer
Expand All @@ -152,20 +153,20 @@ -(void)updateValue:(NSTimer*)timer
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
self.progress += now - self.lastUpdate;
self.lastUpdate = now;

if(self.progress >= self.totalTime)
{
[timer invalidate];
self.progress = self.totalTime;
}

float percent = self.progress / self.totalTime;
float updateVal =[self.counter update:percent];
float value = self.startingValue + (updateVal * (self.destinationValue - self.startingValue));


[self setTextValue:value];

if(self.progress == self.totalTime)
{
[self runCompletionBlock];
Expand Down

0 comments on commit c4dd64c

Please sign in to comment.