【发布时间】:2014-03-19 21:32:24
【问题描述】:
我想像图中一样标记以下曲线的每个部分。标签原点应从曲线段的中间开始,见图。我认为在我的逻辑中有些事情不太正确。您能否帮助我的代码中的错误在哪里?我认为代码和数字说明了自己。
感谢您的宝贵时间和帮助!
CGFloat radius = self.height / 3.2f; // radius of the curve
CGPoint center = CGPointMake(self.width / 2.f, self.height / 2.f); //center of the curve
UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:degreesToRadian(270.f) endAngle:DEGREES_TO_RADIANS(269.999f) clockwise:YES]; // go around like the clock
CGFloat strokeStart = 0.f;
// Add the parts of the curve to the view
for (int i = 0; i < segmentValues.count; i++) {
NSNumber *value = [segmentValues objectAtIndex:i];
CGFloat strokeEnd = [value floatValue] / kPercentHundred;
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = circlePath.CGPath;
circle.lineCap = kCALineCapButt;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [[colors objectAtIndex:i] CGColor];
circle.lineWidth = kWidhtLine;
circle.zPosition = 1.f;
circle.strokeStart = strokeStart;
circle.strokeEnd = strokeStart + strokeEnd;
[self.layer addSublayer:circle];
UIView *example = [[UIView alloc] init];
example.backgroundColor = kColorRed;
example.size = CGSizeMake(10, 10);
// middle = "(circle.strokeStart + circle.strokeEnd) / 2.f"
// * convert percent to degree
CGFloat angle = (circle.strokeStart + circle.strokeEnd) / 2.f * 360.f;
// determine the point and add the right offset
example.origin = CGPointMake(cosf(angle) * radius + center.x, sinf(angle) * radius + center.y);
[self addSubview:example];
strokeStart = circle.strokeEnd;
}
我看到的数字。
我期望的概念类似于下图。
【问题讨论】:
-
您的角度计算看起来不正确。 strokeStart 和 strokeEnd 应该是从 0 到 1 的值。因此,如果要将它们转换为角度,则应使用 (strokeStart+strokeEnd)/2 * 2 * M_PI,简化为 (strokeStart+strokeEnd) * M_PI (角度以弧度表示,范围从 0 到 2 pi,而不是 0 到 360。)
-
是的,你是对的,非常感谢。我还添加了这个“degreesToRadian(270.f)”到角度,现在它可以工作了。您能否将评论移至您的答案?
标签: ios objective-c geometry computational-geometry