CGContextAddArc是什么导致这个边缘条件?

我通过使用iOS Core Graphics在圆的周围绘制圆点来创build视图。 计算点中心,将其存储在数组中,并在呈现到屏幕之前使用CGContextAddArc进行检索。 我对绘制周长点的方法有充分的信心,这些方法可能被[A]概括,[B]被填充一种颜色,[C]被交替地填充和勾画,[D]被填充如所示的五种颜色的序列。

无核弧

但是,如果将中心点添加到arrays中,则在周长上绘制的最后一个点的属性会发生变化。

certificate这种情况的最简单的方法是绘制周长点,其中[E]是小中心点[F]大中心点,[G]是中心点; 在每种情况下,小的和大的居中的点预计将被填满绿色。 使用[H]时,两个中心点都没有被填充,也有问题。

边缘条件

我一直在学习Core Graphics一个月,并需要帮助来确定导致此问题的边缘条件。 我真的很欢迎有经验的核心graphics程序员的见解。 谢谢。

这里是实现代码; 首先,初始化视图设置的方法。

- (void)ViewSettings_WaitView { sectors = 80; // number of dots on perimeter limit = sectors; uberRadius = 52; // radius of large circle perimeter dotRadius = 4; // radius of dots on large circle perimeter dotsFilled = FALSE; // fill every with colour or outline oneColour = FALSE; // every colour or one colour alternateDots = FALSE; // alternately filled and outlined ringDot = 64; // 64:show 0:hide ringDotFilled = TRUE; // fill or outlined centreDot = 26; // 26:show 0:hide centreDotFilled = FALSE; // fill or outlined [self centreReference]; // set up arc drawing to start from 12 o'clock position [self selectZone]; // emulate 1-of-5 colours selected in GlobalView } 

这里是绘制这些图像的代码

 - (void)drawCircle { context = UIGraphicsGetCurrentContext(); // Get the Graphics Context CGContextSetLineWidth(context, 0.5); // Set the circle outerline-width dotPosition = CGPointMake(uberX,uberY); // centre point for ring dot and centre dot // create ring dot (larger centre dot) if (ringDot != 0) { iOSCircle *newCircle = [[iOSCircle alloc] init]; // Create a new iOSCircle Object newCircle.circleRadius = ringDot; // ringDot radius newCircle.circleCentre = dotPosition; // place ringDot on the frame [totalCircles addObject:newCircle]; // add to the circle Array [self setNeedsDisplay]; // update the view NSLog(@"ringDot added:%@ radius: %f", NSStringFromCGPoint(dotPosition), ringDot); } // create centre dot (smaller centre dot) if (centreDot != 0) { iOSCircle *newCircle = [[iOSCircle alloc] init]; // Create a new iOSCircle Object newCircle.circleRadius = centreDot; // ringDot radius newCircle.circleCentre = dotPosition; // place ringDot on the frame [totalCircles addObject:newCircle]; // add to the circle Array [self setNeedsDisplay]; // update the view NSLog(@"centreDot added:%@ radius: %f", NSStringFromCGPoint(dotPosition), centreDot); } // create sector dots (on perimeter of the arc) for (dotCount = 1; dotCount < limit+1; dotCount++) { iOSCircle *newCircle = [[iOSCircle alloc] init]; // Create a new iOSCircle Object newCircle.circleRadius = dotRadius; [self newCentre]; // create a new x and y point for each sector dot dotPosition = CGPointMake(x,y); // create each sector dot newCircle.circleCentre = dotPosition; // place each dot on the frame [totalCircles addObject:newCircle]; // add to the circle Array [self setNeedsDisplay]; // update the view NSLog(@"Dot %i %@", dotCount, NSStringFromCGPoint(dotPosition)); } dotCount = 1; for (iOSCircle *circle in totalCircles) { // Loop through array and retrieve dot dimensions CGContextAddArc(context, circle.circleCentre.x, circle.circleCentre.y, circle.circleRadius, 0.0, M_PI * 2.0, YES); [self renderSectorDots]; // render dots to view dotCount++; } if (ringDot != 0) { NSLog(@"add ringDot %@ radius: %f", NSStringFromCGPoint(dotPosition), ringDot); [self renderRingDot]; } if (centreDot != 0) { NSLog(@"add centreDot %@ radius: %f", NSStringFromCGPoint(dotPosition), centreDot); [self renderCentreDot]; } } 

而渲染中心点的方法(环点相似)

 - (void)renderCentreDot { switch (centreDotFilled) { case 1: colourIndex = selectedZone; [self whatColour]; break; default: [self dotOutline]; break; } } - (void)whatColour { switch (colourIndex) { case 1: // Fill the circle with cyan [self paintCyan]; break; case 2: // Fill the circle with red [self paintRed]; break; case 3: // Fill the circle with yellow [self paintYellow]; break; case 4: // Fill the circle with magenta [self paintMagenta]; break; case 5: // Fill the circle with green [self paintGreen]; break; default: break; } } - (void)dotOutline { CGContextStrokePath(context); // draw outerline only } - (void)paintGreen { CGContextSetFillColorWithColor(context, [[UIColor greenColor] CGColor]); CGContextDrawPath(context, kCGPathFillStroke); // fill with outerline-colour } 

最后是计算扇区点的新中心的方法

 - (void)newCentre { dotAngle = dotAngle + uberAngle; x = uberX + (uberRadius * 2 * cos(dotAngle)); y = uberY + (uberRadius * 2 * sin(dotAngle)); // NSLog(@"%i %f %f %f", dotCount, dotAngle, endAngle, uberAngle); } 

以及一个初始化中心参数并从12点钟位置开始绘制的方法

 - (void)centreReference { uberX = 160; uberY = 240; uberAngle = (2.0 * PI) / sectors; dotAngle = PI * -0.5; // start drawing 0.5 PI radians before 3 o'clock endAngle = PI * 1.5; // stop drawing 1.5 PI radians after 3 o'clock NSLog(@"%f %f %f %f", uberX, ringY, uberRadius, uberAngle); 

}

你有80个外圈和0,1或2个内圈。 所有这些圆都存储在一个名为totalCircles数组中,而内部的圆则存储在数组的前面。 所以如果你有两个内圈, totalCircles有82个元素。

问题是你只画出了前80圈:两个内圈和78个外圈。

然后你试图填充其中一个内圈,而是填写第79个外圈。

换句话说,你正在失去哪个圈子是哪个。


尝试按以下方式构build代码:

你已经有一个“iOSCircle”对象,这是很好的。 这个圆形对象也应该包含它的颜色,并有能力绘制自己:

 @interface Circle : NSObject @property (nonatomic) CGPoint centre; @property (nonatomic) CGFloat radius; @property (nonatomic, strong) UIColor* color; - (void)draw; @end 

-draw方法绘制圆圈,如果设置了color ,则可以填充color

 - (void)draw { CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextAddArc(ctx, self.centre.x, self.centre.y, etc...) if (self.color) { [self.color setFill]; CGContextDrawPath(ctx, kCGPathFillStroke); } else { CGContextStrokePath(ctx); } } 

你应该有一个方法来生成一个圆对象的数组。 这将创build80个外部圆圈,以及可选的内部圆圈。 这也将为每个圆圈分配一个颜色。

你的主要绘图方法就这么简单:

 - (void)drawCircle { NSArray* circles = [self generateCircles]; for (Circle* circle in circles) { [circle draw]; } }