Zhang Hang

Format code

... ... @@ -10,7 +10,7 @@
@interface PNPieChart()
@property (nonatomic, readwrite) NSArray *items;
@property (nonatomic) NSArray *items;
@property (nonatomic) NSArray *endPercentages;
@property (nonatomic) CGFloat outerCircleRadius;
... ... @@ -28,6 +28,7 @@
- (CGFloat)startPercentageForItemAtIndex:(NSUInteger)index;
- (CGFloat)endPercentageForItemAtIndex:(NSUInteger)index;
- (CGFloat)ratioForItemAtIndex:(NSUInteger)index;
- (CAShapeLayer *)newCircleLayerWithRadius:(CGFloat)radius
borderWidth:(CGFloat)borderWidth
fillColor:(UIColor *)fillColor
... ... @@ -45,8 +46,8 @@
self = [self initWithFrame:frame];
if(self){
_items = [NSArray arrayWithArray:items];
_outerCircleRadius = CGRectGetWidth(self.bounds)/2;
_innerCircleRadius = CGRectGetWidth(self.bounds)/6;
_outerCircleRadius = CGRectGetWidth(self.bounds) / 2;
_innerCircleRadius = CGRectGetWidth(self.bounds) / 6;
_descriptionTextColor = [UIColor whiteColor];
_descriptionTextFont = [UIFont fontWithName:@"Avenir-Medium" size:18.0];
... ... @@ -66,10 +67,10 @@
NSMutableArray *endPercentages = [NSMutableArray new];
[_items enumerateObjectsUsingBlock:^(PNPieChartDataItem *item, NSUInteger idx, BOOL *stop) {
if (total == 0){
[endPercentages addObject:@(1.0/_items.count*(idx+1))];
[endPercentages addObject:@(1.0 / _items.count * (idx + 1))];
}else{
currentTotal += item.value;
[endPercentages addObject:@(currentTotal/total)];
[endPercentages addObject:@(currentTotal / total)];
}
}];
self.endPercentages = [endPercentages copy];
... ... @@ -96,7 +97,7 @@
CGFloat startPercnetage = [self startPercentageForItemAtIndex:i];
CGFloat endPercentage = [self endPercentageForItemAtIndex:i];
CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius)/2;
CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2;
CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius;
CAShapeLayer *currentPieLayer = [self newCircleLayerWithRadius:radius
borderWidth:borderWidth
... ... @@ -119,7 +120,7 @@
- (UILabel *)descriptionLabelForItemAtIndex:(NSUInteger)index{
PNPieChartDataItem *currentDataItem = [self dataItemForIndex:index];
CGFloat distance = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2;
CGFloat centerPercentage = ([self startPercentageForItemAtIndex:index] + [self endPercentageForItemAtIndex:index])/2;
CGFloat centerPercentage = ([self startPercentageForItemAtIndex:index] + [self endPercentageForItemAtIndex:index])/ 2;
CGFloat rad = centerPercentage * 2 * M_PI;
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
... ... @@ -139,8 +140,7 @@
descriptionLabel.font = _descriptionTextFont;
CGSize labelSize = [descriptionLabel.text sizeWithAttributes:@{NSFontAttributeName:descriptionLabel.font}];
descriptionLabel.frame = CGRectMake(
descriptionLabel.frame.origin.x, descriptionLabel.frame.origin.y,
descriptionLabel.frame = CGRectMake(descriptionLabel.frame.origin.x, descriptionLabel.frame.origin.y,
descriptionLabel.frame.size.width, labelSize.height);
descriptionLabel.numberOfLines = 0;
descriptionLabel.textColor = _descriptionTextColor;
... ... @@ -198,12 +198,11 @@
circle.lineWidth = borderWidth;
circle.path = path.CGPath;
return circle;
}
- (void)maskChart{
CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius)/2;
CGFloat radius = _innerCircleRadius + (_outerCircleRadius - _innerCircleRadius) / 2;
CGFloat borderWidth = _outerCircleRadius - _innerCircleRadius;
CAShapeLayer *maskLayer = [self newCircleLayerWithRadius:radius
borderWidth:borderWidth
... ... @@ -223,8 +222,11 @@
[maskLayer addAnimation:animation forKey:@"circleAnimation"];
}
- (void)createArcAnimationForLayer:(CAShapeLayer *)layer ForKey:(NSString *)key fromValue:(NSNumber *)from toValue:(NSNumber *)to Delegate:(id)delegate
{
- (void)createArcAnimationForLayer:(CAShapeLayer *)layer
forKey:(NSString *)key
fromValue:(NSNumber *)from
toValue:(NSNumber *)to
delegate:(id)delegate{
CABasicAnimation *arcAnimation = [CABasicAnimation animationWithKeyPath:key];
arcAnimation.fromValue = @0;
arcAnimation.toValue = to;
... ...