Zhang Hang

Format code

@@ -10,14 +10,14 @@ @@ -10,14 +10,14 @@
10 10
11 @interface PNPieChart() 11 @interface PNPieChart()
12 12
13 -@property (nonatomic, readwrite) NSArray *items; 13 +@property (nonatomic) NSArray *items;
14 @property (nonatomic) NSArray *endPercentages; 14 @property (nonatomic) NSArray *endPercentages;
15 15
16 @property (nonatomic) CGFloat outerCircleRadius; 16 @property (nonatomic) CGFloat outerCircleRadius;
17 @property (nonatomic) CGFloat innerCircleRadius; 17 @property (nonatomic) CGFloat innerCircleRadius;
18 18
19 -@property (nonatomic) UIView *contentView; 19 +@property (nonatomic) UIView *contentView;
20 -@property (nonatomic) CAShapeLayer *pieLayer; 20 +@property (nonatomic) CAShapeLayer *pieLayer;
21 @property (nonatomic) NSMutableArray *descriptionLabels; 21 @property (nonatomic) NSMutableArray *descriptionLabels;
22 22
23 23
@@ -28,6 +28,7 @@ @@ -28,6 +28,7 @@
28 - (CGFloat)startPercentageForItemAtIndex:(NSUInteger)index; 28 - (CGFloat)startPercentageForItemAtIndex:(NSUInteger)index;
29 - (CGFloat)endPercentageForItemAtIndex:(NSUInteger)index; 29 - (CGFloat)endPercentageForItemAtIndex:(NSUInteger)index;
30 - (CGFloat)ratioForItemAtIndex:(NSUInteger)index; 30 - (CGFloat)ratioForItemAtIndex:(NSUInteger)index;
  31 +
31 - (CAShapeLayer *)newCircleLayerWithRadius:(CGFloat)radius 32 - (CAShapeLayer *)newCircleLayerWithRadius:(CGFloat)radius
32 borderWidth:(CGFloat)borderWidth 33 borderWidth:(CGFloat)borderWidth
33 fillColor:(UIColor *)fillColor 34 fillColor:(UIColor *)fillColor
@@ -45,12 +46,12 @@ @@ -45,12 +46,12 @@
45 self = [self initWithFrame:frame]; 46 self = [self initWithFrame:frame];
46 if(self){ 47 if(self){
47 _items = [NSArray arrayWithArray:items]; 48 _items = [NSArray arrayWithArray:items];
48 - _outerCircleRadius = CGRectGetWidth(self.bounds)/2; 49 + _outerCircleRadius = CGRectGetWidth(self.bounds) / 2;
49 - _innerCircleRadius = CGRectGetWidth(self.bounds)/6; 50 + _innerCircleRadius = CGRectGetWidth(self.bounds) / 6;
50 51
51 _descriptionTextColor = [UIColor whiteColor]; 52 _descriptionTextColor = [UIColor whiteColor];
52 _descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0]; 53 _descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0];
53 - _descriptionTextShadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.4]; 54 + _descriptionTextShadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
54 _descriptionTextShadowOffset = CGSizeMake(0, 1); 55 _descriptionTextShadowOffset = CGSizeMake(0, 1);
55 _duration = 1.0; 56 _duration = 1.0;
56 57
@@ -66,10 +67,10 @@ @@ -66,10 +67,10 @@
66 NSMutableArray *endPercentages = [NSMutableArray new]; 67 NSMutableArray *endPercentages = [NSMutableArray new];
67 [_items enumerateObjectsUsingBlock:^(PNPieChartDataItem *item, NSUInteger idx, BOOL *stop) { 68 [_items enumerateObjectsUsingBlock:^(PNPieChartDataItem *item, NSUInteger idx, BOOL *stop) {
68 if (total == 0){ 69 if (total == 0){
69 - [endPercentages addObject:@(1.0/_items.count*(idx+1))]; 70 + [endPercentages addObject:@(1.0 / _items.count * (idx + 1))];
70 }else{ 71 }else{
71 currentTotal += item.value; 72 currentTotal += item.value;
72 - [endPercentages addObject:@(currentTotal/total)]; 73 + [endPercentages addObject:@(currentTotal / total)];
73 } 74 }
74 }]; 75 }];
75 self.endPercentages = [endPercentages copy]; 76 self.endPercentages = [endPercentages copy];
@@ -96,7 +97,7 @@ @@ -96,7 +97,7 @@
96 CGFloat startPercnetage = [self startPercentageForItemAtIndex:i]; 97 CGFloat startPercnetage = [self startPercentageForItemAtIndex:i];
97 CGFloat endPercentage = [self endPercentageForItemAtIndex:i]; 98 CGFloat endPercentage = [self endPercentageForItemAtIndex:i];
98 99
99 - CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius)/2; 100 + CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2;
100 CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius; 101 CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius;
101 CAShapeLayer *currentPieLayer = [self newCircleLayerWithRadius:radius 102 CAShapeLayer *currentPieLayer = [self newCircleLayerWithRadius:radius
102 borderWidth:borderWidth 103 borderWidth:borderWidth
@@ -119,7 +120,7 @@ @@ -119,7 +120,7 @@
119 - (UILabel *)descriptionLabelForItemAtIndex:(NSUInteger)index{ 120 - (UILabel *)descriptionLabelForItemAtIndex:(NSUInteger)index{
120 PNPieChartDataItem *currentDataItem = [self dataItemForIndex:index]; 121 PNPieChartDataItem *currentDataItem = [self dataItemForIndex:index];
121 CGFloat distance = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2; 122 CGFloat distance = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2;
122 - CGFloat centerPercentage = ([self startPercentageForItemAtIndex:index] + [self endPercentageForItemAtIndex:index])/2; 123 + CGFloat centerPercentage = ([self startPercentageForItemAtIndex:index] + [self endPercentageForItemAtIndex:index])/ 2;
123 CGFloat rad = centerPercentage * 2 * M_PI; 124 CGFloat rad = centerPercentage * 2 * M_PI;
124 125
125 UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)]; 126 UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
@@ -139,16 +140,15 @@ @@ -139,16 +140,15 @@
139 140
140 descriptionLabel.font = _descriptionTextFont; 141 descriptionLabel.font = _descriptionTextFont;
141 CGSize labelSize = [descriptionLabel.text sizeWithAttributes:@{NSFontAttributeName:descriptionLabel.font}]; 142 CGSize labelSize = [descriptionLabel.text sizeWithAttributes:@{NSFontAttributeName:descriptionLabel.font}];
142 - descriptionLabel.frame = CGRectMake( 143 + descriptionLabel.frame = CGRectMake(descriptionLabel.frame.origin.x, descriptionLabel.frame.origin.y,
143 - descriptionLabel.frame.origin.x, descriptionLabel.frame.origin.y,  
144 descriptionLabel.frame.size.width, labelSize.height); 144 descriptionLabel.frame.size.width, labelSize.height);
145 - descriptionLabel.numberOfLines = 0; 145 + descriptionLabel.numberOfLines = 0;
146 - descriptionLabel.textColor = _descriptionTextColor; 146 + descriptionLabel.textColor = _descriptionTextColor;
147 - descriptionLabel.shadowColor = _descriptionTextShadowColor; 147 + descriptionLabel.shadowColor = _descriptionTextShadowColor;
148 - descriptionLabel.shadowOffset = _descriptionTextShadowOffset; 148 + descriptionLabel.shadowOffset = _descriptionTextShadowOffset;
149 - descriptionLabel.textAlignment = NSTextAlignmentCenter; 149 + descriptionLabel.textAlignment = NSTextAlignmentCenter;
150 - descriptionLabel.center = center; 150 + descriptionLabel.center = center;
151 - descriptionLabel.alpha = 0; 151 + descriptionLabel.alpha = 0;
152 descriptionLabel.backgroundColor = [UIColor clearColor]; 152 descriptionLabel.backgroundColor = [UIColor clearColor];
153 return descriptionLabel; 153 return descriptionLabel;
154 } 154 }
@@ -198,14 +198,13 @@ @@ -198,14 +198,13 @@
198 circle.lineWidth = borderWidth; 198 circle.lineWidth = borderWidth;
199 circle.path = path.CGPath; 199 circle.path = path.CGPath;
200 200
201 -  
202 return circle; 201 return circle;
203 } 202 }
204 203
205 - (void)maskChart{ 204 - (void)maskChart{
206 - CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius)/2; 205 + CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2;
207 CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius; 206 CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius;
208 - CAShapeLayer *maskLayer = [self newCircleLayerWithRadius:radius 207 + CAShapeLayer *maskLayer = [self newCircleLayerWithRadius:radius
209 borderWidth:borderWidth 208 borderWidth:borderWidth
210 fillColor:[UIColor clearColor] 209 fillColor:[UIColor clearColor]
211 borderColor:[UIColor blackColor] 210 borderColor:[UIColor blackColor]
@@ -223,13 +222,16 @@ @@ -223,13 +222,16 @@
223 [maskLayer addAnimation:animation forKey:@"circleAnimation"]; 222 [maskLayer addAnimation:animation forKey:@"circleAnimation"];
224 } 223 }
225 224
226 -- (void)createArcAnimationForLayer:(CAShapeLayer *)layer ForKey:(NSString *)key fromValue:(NSNumber *)from toValue:(NSNumber *)to Delegate:(id)delegate 225 +- (void)createArcAnimationForLayer:(CAShapeLayer *)layer
227 -{ 226 + forKey:(NSString *)key
  227 + fromValue:(NSNumber *)from
  228 + toValue:(NSNumber *)to
  229 + delegate:(id)delegate{
228 CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:key]; 230 CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:key];
229 - arcAnimation.fromValue = @0; 231 + arcAnimation.fromValue = @0;
230 - arcAnimation.toValue = to; 232 + arcAnimation.toValue = to;
231 - arcAnimation.delegate = delegate; 233 + arcAnimation.delegate = delegate;
232 - arcAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]; 234 + arcAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
233 [layer addAnimation:arcAnimation forKey:key]; 235 [layer addAnimation:arcAnimation forKey:key];
234 [layer setValue:to forKey:key]; 236 [layer setValue:to forKey:key];
235 } 237 }