2012-03-29 118 views
1

我正在绘制使用UIBezierPath的六边形,其中点(或交点)变化由滑块控制&。 PLS。在这里看到的截图: http://postimage.org/image/tq8z8t9qb/使用UIBezierPath绘制六边形

现在的问题是,当最后一点绘制或移动它已经错了(我标志着圈 截图)。我认为,这是从贝塞尔路径的起点/中心点画出来的。 希望你可以检查代码:

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(ctx, rect);  
    CGPoint center = CGPointMake(150.0, 150.0); 

    // line 
    UIBezierPath *polyPath = [UIBezierPath bezierPath]; 
    polyPath.lineWidth = 5; 
    [polyPath moveToPoint:CGPointMake(center.x, center.y + 60.0)]; 

    for (int i = 1; i <= 6; ++i) { 
     CGFloat x, y; 

     NSNumber *oNumb = [[self aIndexValues] objectAtIndex:i-1]; 
     fXValue_ = [oNumb floatValue]; 
     x = fXValue_ * sinf(i * 2.0 * M_PI/6.0); 
     y = fXValue_ * cosf(i * 2.0 * M_PI/6.0); 

     [polyPath addLineToPoint:CGPointMake(center.x + x, center.y + y)]; 
    } 

    [polyPath closePath]; 
    [[UIColor redColor] setStroke]; 
    [polyPath stroke]; 

    // circle point 
    for(int i = 1; i <= 6; ++i) { 
     CGFloat x, y; 
     NSNumber *oNumb = [[self aIndexValues] objectAtIndex:i-1]; 
     fXValue_ = [oNumb floatValue];    
     x = fXValue_ * sinf(i * 2.0 * M_PI/6.0); 
     y = fXValue_ * cosf(i * 2.0 * M_PI/6.0); 

     UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(center.x + x - 6, center.y + y - 6, 15, 15)]; 
     circle.lineWidth = 5; 
     [[UIColor whiteColor] setFill]; 
     [circle fill]; 
    } 
} 

请注意,让我知道什么是错的。由于

+0

目前还不清楚你认为应该做什么,所以告诉你什么是错的几乎是不可能的。你能上传你认为应该是什么样子的图片吗? – user1118321 2012-03-29 18:49:55

+0

如果您发布样本容器应用程序,它会帮助我们。 – Senior 2012-03-29 18:53:53

回答

0

仔细看这里:

[polyPath moveToPoint:CGPointMake(center.x, center.y + 60.0)]; 

这总是绘制于六边形的最大下顶点,忽视了该顶点滑块设置。它可能应该使用aIndexValues的适当元素,而不是60.0

一些其他的东西不阻止这个工作,但是让你的代码凌乱:

  1. 你并不需要调用CGContextClearRectdrawRect。上下文已经清除。
  2. 您正在使用名为fXValue_的变量,该变量未在此方法中定义。你不需要从别处获得它的值,所以在这个方法中声明一个变量来存储顶点半径。
  3. 看看您是否可以找到一个干净的方法来计算从0到5而不是1到6的i,因此,无论您何时从aIndexValues检索数字,都必须减去1