【发布时间】:2015-02-07 04:13:14
【问题描述】:
我通过使用 iOS Core Graphics 在圆的周边绘制圆点弧来创建视图。点中心被计算,存储在一个数组中,并在渲染到屏幕之前使用 CGContextAddArc 检索。我对用于绘制周边点的方法充满信心,这些点可能是 [A] 勾勒出轮廓,[B] 填充一种颜色,[C] 交替填充和勾勒轮廓,以及 [D] 填充一系列五种颜色,如图所示。
但是,如果将中心点添加到数组中,则在周长上绘制的最后一个点的属性会发生变化。
演示这种情况的最简单方法是绘制外围点,并加上 [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);
}
【问题讨论】:
标签: ios objective-c core-graphics