0

当用户在我的视图中选择位置 x,y 并且这些点应该等距时,如何在圆形路径中添加 16 个点,

所以当用户在视图中点击一个位置时,我会用 16 个点完成圆圈,见附件。

在此处输入图像描述

该图像是此代码的用途:

CGPoint CenterPoint = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
CGPoint Point;
float Angel = 360/16;

for (int i = 0 ; i < 16;i++)
{
    float distance = [self distanceFrom:newPoint to:centerPoint];
    Point.x = CenterPoint.x + distance * cos(Angel);
    Point.y = CenterPoint.y + distance * sin(Angel);

    CGContextMoveToPoint(cacheContext, Point.x, Point.y);
    CGContextAddLineToPoint(cacheContext, Point.x, Point.y);
    CGContextStrokePath(cacheContext);

    Angel+= 10;
}
4

2 回答 2

0

检查下面的代码它可能对你帮助不大

int startAngle = 0;

while (startAngle < 360)
{
    CAShapeLayer *slice = [CAShapeLayer layer];
    slice.fillColor = _buttonColor.CGColor;
    slice.strokeColor = [UIColor clearColor].CGColor;
    slice.lineWidth = 3.0;

    CGFloat angle = DEGREES_TO_RADIANS(-60.0);
    CGPoint center = CGPointMake(self.frame.size.width/2.0, self.frame.size.height/2.0);
    CGFloat radius = self.frame.size.width/2.0;

    UIBezierPath *piePath = [UIBezierPath bezierPath];
    [piePath moveToPoint:center];

    //[piePath addLineToPoint:CGPointMake(center.x + radius * cosf(DEGREES_TO_RADIANS(startAngle)), center.y + radius * sinf(DEGREES_TO_RADIANS(startAngle)))];

    [piePath addArcWithCenter:center radius:radius startAngle:DEGREES_TO_RADIANS(startAngle) endAngle:DEGREES_TO_RADIANS(startAngle + 63) clockwise:YES];

    //   [piePath addLineToPoint:center];
    [piePath closePath]; // this will automatically add a straight line to the center
    slice.path = piePath.CGPath;

    startAngle += (360/15);

    [self.layer addSublayer:slice];
}
于 2013-02-12T08:25:11.010 回答
0

cosandsin函数期望以弧度而不是度数(您提供的)为单位的角度。请尝试这种替代方法。

float distance = [self distanceFrom:newPoint to:centerPoint];
CGContextSaveGState(cacheContext);
CGContextTranslateCTM(cacheContext, CenterPoint.x, CenterPoint.y);

for (int i = 0 ; i < 16;i++)
{
    float angle = i * (M_2_PI / 16);
    CGPoint pt = CGPointMake(distance * cos(angle), distance * sin(angle));

    CGContextMoveToPoint(cacheContext, 0, 0);
    CGContextAddLineToPoint(cacheContext, pt.x, pt.y);
    CGContextStrokePath(cacheContext);
}
CGContextRestoreGState(cacheContext);

这种方法可以转换上下文,因此原点(也称为中心点)确实是,CenterPoint因此您无需担心添加CenterPoint任何内容。由于distance不受循环内任何内容的影响,因此应将其移出,以免不必要地重新计算。

于 2013-02-12T10:14:26.737 回答