gpt4 book ai didi

ios - iOS 中的动画饼图

转载 作者:可可西里 更新时间:2023-11-01 04:35:10 25 4
gpt4 key购买 nike

我正在尝试在 iOS 中创建一个基本上像这样的动画饼图:

Pie chart progression

简而言之,它以灰色圆圈开始,随着动画的进行,箭头围绕圆圈移动,直到达到我指定的百分比。

我使用了 Zachary Waldowski 在这个 SO 问题中发布的示例代码:

Animated CAShapeLayer Pie

这让我到了可以创建基本动画的地步。栗色馅饼长到正确的大小。我正在努力解决的问题是如何将箭头映射到动画,以便它随着饼图的增长而被拖动。

我有什么想法可以实现吗?

最佳答案

好的,我找到了解决方案。

以工作为基础 Zachary Waldowski创建(请参阅我的原始帖子)后,我能够在 CALayer 中完成所有操作。

简而言之,我用栗色画了一个外圆,用浅灰色画了一个较小的圆,用白色画了一条描边路径,然后用手画了箭头尖端的三角形。

下面是执行魔法的相关代码部分:

- (void)drawInContext:(CGContextRef)context {
CGRect circleRect = CGRectInset(self.bounds, 1, 1);
CGFloat startAngle = -M_PI / 2;
CGFloat endAngle = self.progress * 2 * M_PI + startAngle;

CGColorRef outerPieColor = [[UIColor colorWithRed: 137.0 / 255.0 green: 12.0 / 255.0 blue: 88.0 / 255.0 alpha: 1.0] CGColor];
CGColorRef innerPieColor = [[UIColor colorWithRed: 235.0 / 255.0 green: 214.0 / 255.0 blue: 227.0 / 255.0 alpha: 1.0] CGColor];
CGColorRef arrowColor = [[UIColor whiteColor] CGColor];

// Draw outer pie
CGFloat outerRadius = CGRectGetMidX(circleRect);
CGPoint center = CGPointMake(CGRectGetMidX(circleRect), CGRectGetMidY(circleRect));

CGContextSetFillColorWithColor(context, outerPieColor);
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, outerRadius, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextFillPath(context);

// Draw inner pie
CGFloat innerRadius = CGRectGetMidX(circleRect) * 0.45;

CGContextSetFillColorWithColor(context, innerPieColor);
CGContextMoveToPoint(context, center.x, center.y);
CGContextAddArc(context, center.x, center.y, innerRadius, startAngle, endAngle, 0);
CGContextClosePath(context);
CGContextFillPath(context);

// Draw the White Line
CGFloat lineRadius = CGRectGetMidX(circleRect) * 0.72;
CGFloat arrowWidth = 0.35;

CGContextSetStrokeColorWithColor(context, arrowColor);
CGContextSetFillColorWithColor(context, arrowColor);

CGMutablePathRef path = CGPathCreateMutable();
CGContextSetLineWidth(context, 16);
CGFloat lineEndAngle = ((endAngle - startAngle) >= arrowWidth) ? endAngle - arrowWidth : endAngle;
CGPathAddArc(path, NULL, center.x, center.y, lineRadius, startAngle, lineEndAngle, 0);
CGContextAddPath(context, path);
CGContextStrokePath(context);

// Draw the Triangle pointer
CGFloat arrowStartAngle = lineEndAngle - 0.01;
CGFloat arrowOuterRadius = CGRectGetMidX(circleRect) * 0.90;
CGFloat arrowInnerRadius = CGRectGetMidX(circleRect) * 0.54;

CGFloat arrowX = center.x + (arrowOuterRadius * cosf(arrowStartAngle));
CGFloat arrowY = center.y + (arrowOuterRadius * sinf(arrowStartAngle));

CGContextMoveToPoint (context, arrowX, arrowY); // top corner

arrowX = center.x + (arrowInnerRadius * cosf(arrowStartAngle));
arrowY = center.y + (arrowInnerRadius * sinf(arrowStartAngle));

CGContextAddLineToPoint(context, arrowX, arrowY); // bottom corner

arrowX = center.x + (lineRadius * cosf(endAngle));
arrowY = center.y + (lineRadius * sinf(endAngle));

CGContextAddLineToPoint(context, arrowX, arrowY); // point
CGContextClosePath(context);
CGContextFillPath(context);

[super drawInContext: context];
}

关于ios - iOS 中的动画饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20952548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com