核心图形 - 以一定角度绘制线条

时间:2011-05-06 15:57:36

标签: iphone objective-c core-graphics

我一直在寻找挣扎这一段时间,需要一些帮助。我只想从我的显示器中心(160,200)以14.4度的间隔绘制25条线。我一直在for循环中使用这行代码,其中x是14.4度乘数 -

    UIImage*backgroundImage = [UIImage imageNamed:@"Primate Background Only.png"];
[backgroundImage drawInRect:CGRectMake(0, 0, 320, 480)];

//绘制外圈

rect = CGRectMake(0.0, 35.0, 320.0, 320.0);     
CGContextRef contextRef = UIGraphicsGetCurrentContext();                // Get the contextRef
CGContextSetLineWidth       (contextRef, 0.5);                  // Set the border width
CGContextSetRGBFillColor    (contextRef, (219.0f/255.0f), (219.0f/255.0f), (219.0f/255.0f), 0.05f);             // Set the circle fill color to GREEN
CGContextSetRGBStrokeColor  (contextRef, 0.0, 0.0, 0.0, 0.2);           // Set the circle border color to BLACK     
CGContextFillEllipseInRect      (contextRef, rect);                 // Fill the circle with the fill color
CGContextStrokeEllipseInRect    (contextRef, rect);                 // Draw the circle border

//绘制内圈

rect = CGRectMake(25.0, 60.0, 270.0, 270.0);                        // Get the contextRef
CGContextSetLineWidth       (contextRef, 0.5);                  // Set the border width
CGContextSetRGBFillColor    (contextRef, (219.0f/255.0f), (219.0f/255.0f), (219.0f/255.0f), 0.25f);
CGContextSetRGBStrokeColor  (contextRef, 0.0, 0.0, 0.0, 0.2);           // Set the circle border color to BLACK     
CGContextFillEllipseInRect      (contextRef, rect);                 // Fill the circle with the fill color
CGContextStrokeEllipseInRect    (contextRef, rect);     

//绘制细分

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0.0, 0.0);           
    for (x=1; x<26; x++) {  

        CGContextSetLineWidth       (context, 0.5);
        CGContextSetRGBStrokeColor  (context, 0.0, 0.0, 0.0, 0.25);
        CGContextMoveToPoint        (context, 160, 200);                
        CGContextAddLineToPoint     (context, (160.0 * (cos((x*14.4)*(M_PI/180)))), (160.0 * (sin((x*14.4)*(M_PI/180)))));                      //  The angle is in degrees
        CGContextAddLineToPoint     (context, 200, 65);
        CGContextAddLineToPoint     (context, 160, 200);            
        CGContextStrokePath(context);                           // Why is this not showing both line color and infill?
        CGContextSetFillColorWithColor   (context, [UIColor whiteColor].CGColor);       
        CGContextFillPath           (context);
        CGContextRef context = UIGraphicsGetCurrentContext();
    }           

(我打算在这里张贴图片,但不允许我这样做!)

有人可以纠正我的触发功能。这意味着从12:00点到24点顺时针画25条线。相反,它向后抽取只有90度然后返回,所有线条的长度也都很长。

非常感谢

以上是用于根据要求绘制两个外圆和内部线段的更大代码示例。我会尝试下一步上传图片。

enter image description here

1 个答案:

答案 0 :(得分:17)

您的主要问题是这行代码:

CGContextAddLineToPoint(context, (160.0 * (cos((x*14.4)*(M_PI/180)))), 
                                (160.0 * (sin((x*14.4)*(M_PI/180)))));

它应该是:

CGContextAddLineToPoint(context, 160 + (160.0 * (cos((x*14.4)*(M_PI/180)))), 
                                200 + (160.0 * (sin((x*14.4)*(M_PI/180)))));

正如你最初写的那样,线条是相对于0,0绘制的,但是你想要相对于你的中心点(160,200)绘制。

接下来的两行也有点怀疑:

        CGContextAddLineToPoint     (context, 200, 65);
        CGContextAddLineToPoint     (context, 160, 200);

我不清楚你在这里尝试做什么,但最终会从每个手臂末端画一条线到一个固定点(200,65)并从那里回到你的中心点,几乎肯定不是你的意图!尝试评论这些线条,确认正确绘制“手臂”并从那里取出......

希望这很清楚

如果您进行了这些更正,您的屏幕将如下所示: Screenshot

相关问题