我通过使用iOS Core Graphics在圆周周围绘制圆弧来创建视图。计算点中心,存储在数组中并使用 CGContextAddArc 进行检索,然后再渲染到屏幕。我完全相信用于绘制周边点的方法,可以是[A]轮廓,[B]填充一种颜色,[C]交替填充和轮廓,[D]填充五种颜色的序列,如图所示。 / p>
但是,如果将中心点添加到阵列中,则在周边绘制的最后点的属性会发生变化。
证明这种情况的最简单方法是绘制周边点,通过添加[E]小中心点[F],大中心点和[G]两个居中点;在每种情况下,预期小的和大的中心点都被填充为绿色。使用[H]时,如果两个居中点的轮廓未填充,也会出现问题。
我一直在学习Core 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);
}
答案 0 :(得分:1)
您有80个外圈和0,1或2个内圈。所有这些圆圈都存储在一个名为totalCircles
的数组中,内圆存储在数组的前面。因此,如果你有两个内圈,totalCircles
有82个元素。
问题在于你只绘制了前80个圆圈:两个内圈和78个外圈。
然后你尝试填充其中一个内圈,而是填入第79个外圈。
换句话说,你正在忘记哪些圈子是哪个。
尝试按以下方式构建代码:
你已经有了一个“iOSCircle”对象,这很好。这个圆形对象也应该包含它的颜色并且能够绘制自己:
@interface Circle : NSObject
@property (nonatomic) CGPoint centre;
@property (nonatomic) CGFloat radius;
@property (nonatomic, strong) UIColor* color;
- (void)draw;
@end
-draw
方法绘制圆圈,如果已设置,则可选择填充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);
}
}
您应该有一个生成圆形对象数组的方法。这将创建80个外圈,以及可选的内圈。这也将为每个圆圈指定一种颜色。
您的主要绘图方法就这么简单:
- (void)drawCircle
{
NSArray* circles = [self generateCircles];
for (Circle* circle in circles)
{
[circle draw];
}
}