当使用相同坐标绘制的图像形成圆弧时,以CGRect绘制的文本标签形成椭圆弧

我是新来的Core Graphics,并试图理解为什么我用CGRect绘制的文本标签形成一个椭圆弧,当我使用相同的坐标绘制图像形成一个圆弧。

Arthur Knopper的原始代码在屏幕触摸处创build圆圈。 通过删除触摸方法,我已经能够沿着一个圆弧(超级圆)产生一系列小圆(点)。 每个圆点都位于超级圆周的中心(如下所示)。

über圆16个点

为了标记每个点,我使用了用于放置点的相同点坐标。 但是,即使点形成一个圆弧,文本标签也会形成一个椭圆弧(如下所示)。 点填充时,标签也被点所隐藏。 这是一个完整的谜。

作为一个新手,我可能会缺less一些基本的核心graphics。 如果任何人都可以解释这是什么,我需要做什么,使圆弧圆形和地方的标签放在顶部,我将不胜感激。

谢谢。

标签弧和点弧

这是代码。

circleView.h NSMutableArray *totalCircles; int dotCount, limit; float uberX, uberY, uberRadius, uberAngle, labelX, labelY,dotRadius, dotsFilled, sectors, x, y; CGPoint dotPosition; CGRect boxBoundary; CGContextRef context; } - (void)demo; @end 

而且…

 -@implementation iOSCircleView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code totalCircles = [[NSMutableArray alloc] init]; // Set background color self.backgroundColor = [UIColor whiteColor]; } return self; } // frame a view for drawing - (void)drawRect:(CGRect)rect { [self demo]; } - (void)demo { context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 0.5); uberX = 120; uberY = 160; uberRadius = 30; sectors = 16; uberAngle = (2.0 * PI) / sectors; dotRadius = 20; dotsFilled = FALSE; for (dotCount = 1; dotCount <= sectors; dotCount++) { // Create a new iOSCircle Object iOSCircle *newCircle = [[iOSCircle alloc] init]; newCircle.circleRadius = dotRadius; [self setSectorDotCoordinates]; // make new point for each dot dotPosition = CGPointMake(x,y); // create each dot NSLog(@"Circle%i: %@", dotCount, NSStringFromCGPoint(dotPosition)); [self autoLabel]; // text hides behind the dots newCircle.circleCentre = dotPosition; // place each dot on the frame [totalCircles addObject:newCircle]; [self setNeedsDisplay]; } CGContextSetShadowWithColor(context, CGSizeMake(-3 , 2), 4.0, [UIColor clearColor].CGColor); dotCount = 1; for (iOSCircle *circle in totalCircles) { CGContextAddArc(context, circle.circleCentre.x, circle.circleCentre.y, circle.circleRadius, 0.0, M_PI * 2.0, YES); // draw the circles NSLog(@"Dot %i Filled %i ", dotCount, dotsFilled); switch (dotsFilled) { case 1: CGContextSetFillColorWithColor(context, [[UIColor cyanColor] CGColor]); //CGContextDrawPath(context, kCGPathFillStroke); break; default: //CGContextStrokePath(context); // draw dot outline break; } dotCount++; } } // draw circular dots in circular patterns - (void)setSectorDotCoordinates { x = uberX + (uberRadius * cos(uberAngle *dotCount) * 2); y = uberY + (uberRadius * sin(uberAngle *dotCount) * 2); } // calculate dot coordinates along a circular arc - (void)autoLabel { CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]); boxBoundary = CGRectMake(x-dotRadius, y-dotRadius, x+dotRadius, y+dotRadius); [[NSString stringWithFormat:@"%i",dotCount] drawInRect:boxBoundary withFont:[UIFont systemFontOfSize:24] lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentCenter]; } 

改变boxBoundary中的autoLabelCGRectMake用一个点坐标和宽度和高度创build一个矩形,而不是两个点:

 (void)autoLabel { CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]); boxBoundary = CGRectMake(x-dotRadius, y-dotRadius, dotRadius*2, dotRadius*2); [[NSString stringWithFormat:@"%i",dotCount] drawInRect:boxBoundary withFont:[UIFont systemFontOfSize:24] lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentCenter]; } 

在你的代码中,“方块”包含的文字越来越大,当你在哪里去的权利。 (宽度和高度不固定)