【问题标题】:drawRect not respondingdrawRect没有响应
【发布时间】:2012-02-20 13:35:53
【问题描述】:

我正在尝试使用存储在 NSArrary 中的一组点来绘制五边形,但是,视图是空的,没有任何错误...

-(void)prepareVertex:(int)noOfVertex
{
  polygonPoint=[NSMutableArray arrayWithCapacity:noOfVertex];
  for(int i=0;i<noOfVertex;i++)
  {
    CGPoint point=CGPointMake(sin((2*M_PI)*i/noOfVertex),(cos(2*M_PI)*i/noOfVertex));
    [polygonPoint addObject:[NSValue valueWithCGPoint:point]]; 
    NSValue *tempVal=[polygonPoint objectAtIndex:i];
    CGPoint tempPoint=[tempVal CGPointValue];
    NSLog(@"%f,%f",tempPoint.x,tempPoint.y);
  }

}

- (void)drawRect:(CGRect)rect
{

  [self prepareVertex:5];    
  for (int i=0; i<5; i++) {
    NSValue *tempVal=[polygonPoint objectAtIndex:i];
    CGPoint tempPoint=[tempVal CGPointValue];
  }


  CGContextRef context=UIGraphicsGetCurrentContext();
  CGContextSetLineWidth(context, 5);
  CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
  CGContextMoveToPoint(context, [[polygonPoint objectAtIndex:0] CGPointValue].x, [[polygonPoint objectAtIndex:0] CGPointValue].x);
  for (int i=1; i<5; i++) {
    CGPoint point=[[polygonPoint objectAtIndex:i] CGPointValue];
    CGContextAddLineToPoint(context,point.x, point.y);
  }
  CGContextStrokePath(context);
}

谁能告诉我这是怎么回事?

【问题讨论】:

    标签: iphone objective-c ios uiview core-graphics


    【解决方案1】:

    嗯,首先要注意的是你使用黑色来描边多边形,所以如果你的视图背景也是黑色的,你什么都看不到。

    然后,我认为这是真正的问题,sin(x) 和 cos(x) 总是在 -1 和 1 之间,所以你生成的点:

    CGPoint point=CGPointMake(sin((2*M_PI)*i/noOfVertex),(cos(2*M_PI)*i/noOfVertex));
    

    都位于矩形 CGrectMake(-1, -1, 2, 2) 中。您的视图区域很可能被状态栏隐藏了。

    因此,如果您生成的坐标是正确的,您可以尝试移除状态栏或更改视图的坐标。但我认为你真正应该做的是将前一行改为这样的:

    CGFloat x = centerPoint.x + radius * sin(2*M_PI*i/noOfVertex);
    CGFloat y = centerPoint.y + radius * cos(2*M_PI*i/noOfVertex);
    CGPoint point = CGPointMake(x, y);
    

    【讨论】:

      【解决方案2】:

      它在视图的左上角绘制一些东西。这个数字太小了,不容易看到。这是因为 polygonPoint 中的值(顺便说一句,您至少应该调用数组 polygonPoints)在 -1.0 和 1.0 之间。您必须将您的点转换到视图的中心并相应地缩放大小。

      有点像

      - (void)drawRect:(CGRect)rect {
          [self prepareVertex:5];    
      
          CGContextRef context=UIGraphicsGetCurrentContext();
          CGContextSetLineWidth(context, 5);
          CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
      
          CGFloat x = self.center.x + self.bounds.size.width * 0.5 * [[polygonPoint objectAtIndex:0] CGPointValue].x;
          CGFloat y = self.center.y + self.bounds.size.height * 0.5 * [[polygonPoint objectAtIndex:0] CGPointValue].y;
          NSLog(@"Point %f/%f", x, y);
          CGContextMoveToPoint(context, x, y);
      
          for (int i=1; i<5; i++) {
              CGPoint point=[[polygonPoint objectAtIndex:i] CGPointValue];
              x = self.center.x + self.bounds.size.width * 0.5 * point.x;
              y = self.center.y + self.bounds.size.height * 0.5 * point.y;
              NSLog(@"Point %f/%f", x, y);
              CGContextAddLineToPoint(context, x, y);
          };
          CGContextStrokePath(context);
      }
      

      成功了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 2013-02-06
        • 2020-06-23
        相关资源
        最近更新 更多