@@ -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