Skip to content

Commit 2c0e2d5

Browse files
author
Eric Rolf
committed
Updated Obj-C version with new animation methods.
1 parent 9fac524 commit 2c0e2d5

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

ProgressView/CircleProgressView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
@import UIKit;
10+
@import QuartzCore;
1011

1112
IB_DESIGNABLE
1213

@@ -25,4 +26,5 @@ IB_DESIGNABLE
2526

2627
@property (nonatomic, strong) UIView *contentView;
2728

29+
- (void)setProgress:(double)progress animated:(BOOL)animated;
2830
@end

ProgressView/CircleProgressView.m

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ @interface CircleProgressView ()
1818
double twoSeventyDegrees; // = 270.0
1919
};
2020

21-
@property (nonatomic, readwrite) double internalProgress;
22-
@property (nonatomic, readwrite) struct Constants constants;
21+
@property (nonatomic, readwrite) double internalProgress;
22+
@property (nonatomic, readwrite) struct Constants constants;
23+
@property (nonatomic, readwrite) double destinationProgress;
24+
@property (nonatomic, strong) CADisplayLink *displayLink;
2325

2426
@end
2527

@@ -62,6 +64,8 @@ - (void)prepareForInterfaceBuilder {
6264
#pragma mark - Setup
6365

6466
- (void)setup {
67+
self.destinationProgress = 0.0;
68+
self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkTick:)];
6569
self.contentView = [UIView new];
6670
self.constants = [self initConstants];
6771

@@ -192,4 +196,35 @@ - (void)drawRect:(CGRect)rect {
192196
}
193197
}
194198

199+
- (void)setProgress:(double)progress animated:(BOOL)animated {
200+
if (animated) {
201+
self.destinationProgress = progress;
202+
[self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
203+
} else {
204+
self.progress = progress;
205+
}
206+
}
207+
208+
- (void)displayLinkTick:(CADisplayLink *)sender {
209+
NSTimeInterval renderTime = sender.duration;
210+
211+
if (self.destinationProgress > self.progress) {
212+
self.progress += renderTime;
213+
if (self.progress >= self.destinationProgress) {
214+
self.progress = self.destinationProgress;
215+
[self.displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
216+
return;
217+
}
218+
}
219+
220+
if (self.destinationProgress < self.progress) {
221+
self.progress -= renderTime;
222+
if (self.progress <= self.destinationProgress) {
223+
self.progress = self.destinationProgress;
224+
[self.displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
225+
return;
226+
}
227+
}
228+
}
229+
195230
@end

0 commit comments

Comments
 (0)