我试图绘制一个五边形,并将一组点存储在 NSArrary 中,但是, View 是空的,没有任何错误......
-(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);
}
谁能告诉我发生了什么事吗?
最佳答案
首先要注意的是,您使用黑色来描边多边形,因此如果您的 View 背景也是黑色,您将看不到任何东西。
然后,我认为这是真正的问题,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)内。您 View 的该区域很可能被状态栏隐藏。
因此,如果您生成的坐标是您要查找的正确坐标,您可以尝试删除状态栏或更改 View 的坐标。但我认为你真正应该做的是将前一行更改为如下所示:
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);
关于iphone - 绘制矩形没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9362034/