先上效果图:
Demo在此,休得撒野!!!!
1.怎么用
/**
* Pie
*
* @param frame frame
* @param dataItems 数据源,此Demo是红、绿、蓝比例为1:4:5
* @param colorItems 对应数据的pie的颜色,如果colorItems.count < dataItems 或
* colorItems 为nil 会随机填充颜色
*
*/
- (id)initWithFrame:(CGRect)frame
dataItems:(NSArray *)dataItems
colorItems:(NSArray *)colorItems;
/**
* 开始动画
*/
- (void)stroke;
2.算法分析
以红色部分开始为例计算
r:扇形半径
center:PieView中心位置
start:开始位置,范围[0,1]
end:结束位置,范围[0,1]
⍺:相对于y轴(-π/2)扇形中间位置的角度
⍺ = 2 * π * (start + 1/2.0 * (end - start)) = π * (start + end)
𝛥x:labelCenter到y轴距离
𝛥x = 1/2.0 * r * sin(⍺)
𝛥y:labelCenter到y轴距离
𝛥y = 1/2.0 * r * cos(⍺)
labelCenter:百分比label的center
labelCenterX = center.x + 𝛥x
labelCenterY = center.y - 𝛥y
3. PieView.m部分
- (id)initWithFrame:(CGRect)frame dataItems:(NSArray *)dataItems colorItems:(NSArray *)colorItems
{
self = [super initWithFrame:frame];
if (self) {
self.hidden = YES;
self.backgroundColor = kPieBackgroundColor;
//1.pieView中心点
CGFloat centerWidth = self.frame.size.width * 0.5f;
CGFloat centerHeight = self.frame.size.height * 0.5f;
CGFloat centerX = centerWidth;
CGFloat centerY = centerHeight;
CGPoint centerPoint = CGPointMake(centerX, centerY);
CGFloat radiusBasic = centerWidth > centerHeight ? centerHeight : centerWidth;
//计算红绿蓝部分总和
_total = 0.0f;
for (int i = 0; i < dataItems.count; i++) {
_total += [dataItems[i] floatValue];
}
//线的半径为扇形半径的一半,线宽是扇形半径,这样就能画出圆形了
//2.背景路径
CGFloat bgRadius = radiusBasic * 0.5;
UIBezierPath *bgPath = [UIBezierPath bezierPathWithArcCenter:centerPoint
radius:bgRadius
startAngle:-M_PI_2
endAngle:M_PI_2 * 3
clockwise:YES];
_bgCircleLayer = [CAShapeLayer layer];
_bgCircleLayer.fillColor = [UIColor clearColor].CGColor;
_bgCircleLayer.strokeColor = [UIColor lightGrayColor].CGColor;
_bgCircleLayer.strokeStart = 0.0f;
_bgCircleLayer.strokeEnd = 1.0f;
_bgCircleLayer.zPosition = 1;
_bgCircleLayer.lineWidth = bgRadius * 2.0f;
_bgCircleLayer.path = bgPath.CGPath;
//3.子扇区路径
CGFloat otherRadius = radiusBasic * 0.5 - 3.0;
UIBezierPath *otherPath = [UIBezierPath bezierPathWithArcCenter:centerPoint
radius:otherRadius
startAngle:-M_PI_2
endAngle:M_PI_2 * 3
clockwise:YES];
CGFloat start = 0.0f;
CGFloat end = 0.0f;
for (int i = 0; i < dataItems.count; i++) {
//4.计算当前end位置 = 上一个结束位置 + 当前部分百分比
end = [dataItems[i] floatValue] / _total + start;
//图层
CAShapeLayer *pie = [CAShapeLayer layer];
[self.layer addSublayer:pie];
pie.fillColor = kPieFillColor;
if (i > colorItems.count - 1 || !colorItems || colorItems.count == 0) {//如果传过来的颜色数组少于item个数则随机填充颜色
pie.strokeColor = kPieRandColor.CGColor;
} else {
pie.strokeColor = ((UIColor *)colorItems[i]).CGColor;
}
pie.strokeStart = start;
pie.strokeEnd = end;
pie.lineWidth = otherRadius * 2.0f;
pie.zPosition = 2;
pie.path = otherPath.CGPath;
//计算百分比label的位置
CGFloat centerAngle = M_PI * (start + end);
CGFloat labelCenterX = kLabelLoctionRatio * sinf(centerAngle) + centerX;
CGFloat labelCenterY = -kLabelLoctionRatio * cosf(centerAngle) + centerY;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, radiusBasic * 0.7f, radiusBasic * 0.7f)];
label.center = CGPointMake(labelCenterX, labelCenterY);
label.text = [NSString stringWithFormat:@"%ld%%",(NSInteger)((end - start + 0.005) * 100)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.layer.zPosition = 3;
[self addSubview:label];
//计算下一个start位置 = 当前end位置
start = end;
}
self.layer.mask = _bgCircleLayer;
}
return self;
}
</br>
- (void)stroke
{
//画图动画
self.hidden = NO;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
animation.duration = kAnimationDuration;
animation.fromValue = @0.0f;
animation.toValue = @1.0f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.removedOnCompletion = YES;
[_bgCircleLayer addAnimation:animation forKey:@"circleAnimation"];
}
4. 结束
颇多不足,敬请指教!!