Skip to content

Commit

Permalink
Replace NSTimer-backed update mechanism with CADisplayLink for more r…
Browse files Browse the repository at this point in the history
…eliable updates
  • Loading branch information
nathan spindel committed Jan 25, 2016
1 parent 526c36e commit 59ac843
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CountingTestProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
33EFDA461C56C4C40084A36A /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 33EFDA451C56C4C40084A36A /* QuartzCore.framework */; };
4C5DC2A416C576E30043204D /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C5DC2A316C576E30043204D /* UIKit.framework */; };
4C5DC2A616C576E30043204D /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C5DC2A516C576E30043204D /* Foundation.framework */; };
4C5DC2A816C576E30043204D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C5DC2A716C576E30043204D /* CoreGraphics.framework */; };
Expand All @@ -22,6 +23,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
33EFDA451C56C4C40084A36A /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
4C5DC2A016C576E30043204D /* CountingTestProject.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CountingTestProject.app; sourceTree = BUILT_PRODUCTS_DIR; };
4C5DC2A316C576E30043204D /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
4C5DC2A516C576E30043204D /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand All @@ -47,6 +49,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
33EFDA461C56C4C40084A36A /* QuartzCore.framework in Frameworks */,
4C5DC2A416C576E30043204D /* UIKit.framework in Frameworks */,
4C5DC2A616C576E30043204D /* Foundation.framework in Frameworks */,
4C5DC2A816C576E30043204D /* CoreGraphics.framework in Frameworks */,
Expand Down Expand Up @@ -76,6 +79,7 @@
4C5DC2A216C576E30043204D /* Frameworks */ = {
isa = PBXGroup;
children = (
33EFDA451C56C4C40084A36A /* QuartzCore.framework */,
4C5DC2A316C576E30043204D /* UIKit.framework */,
4C5DC2A516C576E30043204D /* Foundation.framework */,
4C5DC2A716C576E30043204D /* CoreGraphics.framework */,
Expand Down
11 changes: 7 additions & 4 deletions UICountingLabel.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#import <QuartzCore/QuartzCore.h>

#import "UICountingLabel.h"

#if !__has_feature(objc_arc)
Expand Down Expand Up @@ -86,7 +88,7 @@ @interface UICountingLabel ()
@property NSTimeInterval totalTime;
@property CGFloat easingRate;

@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic, strong) CADisplayLink *timer;
@property (nonatomic, strong) id<UILabelCounter> counter;

@end
Expand Down Expand Up @@ -142,9 +144,10 @@ -(void)countFrom:(CGFloat)startValue to:(CGFloat)endValue withDuration:(NSTimeIn
break;
}

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];
CADisplayLink *timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateValue:)];
timer.frameInterval = 2;
[timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode];
self.timer = timer;
}

Expand Down

0 comments on commit 59ac843

Please sign in to comment.