forked from nktpro/react-native-eximage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircleProgressIndicator.m
More file actions
125 lines (105 loc) · 3.95 KB
/
CircleProgressIndicator.m
File metadata and controls
125 lines (105 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// CircleProgressIndicator.m
// RCTExImage
//
// Created by 郭栋 on 15/7/9.
// Copyright (c) 2015年 guodong. All rights reserved.
//
#import "CircleProgressIndicator.h"
#define kProgressSize 30
@implementation CircleProgressIndicator
{
CAShapeLayer *_maskLayer;
CAShapeLayer *_progressLayer;
CABasicAnimation *_progressAnimation;
}
- (instancetype)init {
self = [super initWithFrame:CGRectZero];
if (self) {
[self _setUp];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:CGRectZero];
if (self) {
[self _setUp];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
[self _updateLayersFrame];
}
- (void)setProgress:(CGFloat)progress {
if (progress > 1.0) {
progress = 1.0;
}
_progress = progress;
_progressLayer.strokeEnd = progress;
// [_progressLayer removeAllAnimations];
// CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
// animation.toValue = [NSNumber numberWithFloat:progress];
// animation.duration = 0.1;
// animation.removedOnCompletion = NO;
// animation.additive = YES;
// animation.fillMode = kCAFillModeForwards;
// animation.delegate = self;
// [_progressLayer addAnimation:animation forKey:@"strokeEnd"];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
_backgroundColor = backgroundColor;
_progressLayer.backgroundColor = backgroundColor.CGColor;
}
- (void)setForegroundColor:(UIColor *)foregroundColor {
_foregroundColor = foregroundColor;
_progressLayer.strokeColor = foregroundColor.CGColor;
}
- (void)_setUp {
[self _initProgressLayers];
[self _updateLayersFrame];
}
- (void)_initProgressLayers {
_progressLayer = [CAShapeLayer layer];
_progressLayer.backgroundColor = [UIColor clearColor].CGColor;
_progressLayer.fillColor = nil;
_progressLayer.strokeColor = [UIColor blackColor].CGColor;
_progressLayer.lineWidth = 2.0;
_progressLayer.lineCap = kCALineCapRound;
_progressLayer.strokeStart = 0.0;
_progressLayer.strokeEnd = 0.0;
_progressLayer.allowsEdgeAntialiasing = YES;
_maskLayer = [CAShapeLayer layer];
_maskLayer.backgroundColor = [UIColor clearColor].CGColor;
_maskLayer.fillColor = nil;
_maskLayer.strokeColor = [UIColor blackColor].CGColor;
_maskLayer.lineWidth = 2.0;
_maskLayer.lineCap = kCALineCapRound;
_maskLayer.strokeStart = 0.0;
_maskLayer.strokeEnd = 1.0;
_maskLayer.allowsEdgeAntialiasing = YES;
_progressLayer.mask = _maskLayer;
[self.layer addSublayer:_progressLayer];
}
- (void)_updateLayersFrame {
_progressLayer.frame = self.bounds;
CGRect frame = CGRectMake((CGRectGetWidth(self.frame) - kProgressSize) / 2,
(CGRectGetHeight(self.frame) - kProgressSize) / 2,
kProgressSize, kProgressSize);
CGFloat startAngle = -M_PI_2;
CGFloat endAngle = M_PI_2 * 3;
CGPoint centerPoint = CGPointMake(frame.origin.x + CGRectGetWidth(frame) / 2, frame.origin.y + CGRectGetHeight(frame) / 2);
_maskLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint
radius:CGRectGetWidth(frame) / 2
startAngle:startAngle
endAngle:endAngle
clockwise:true].CGPath;
_progressLayer.path = [UIBezierPath bezierPathWithArcCenter:centerPoint
radius:CGRectGetWidth(frame) / 2
startAngle:startAngle
endAngle:endAngle
clockwise:true].CGPath;
}
@end