全。很抱歉,如果这个问题已经被问过并回答了,我在搜索时找不到这个特定的问题。
我正在尝试创建一个circle类,它动态地在一个圆内创建一个圆环,并且在它背后的trig有问题。这个想法是为旋转电话创建一个图像,但我希望能够重用这个类来处理其他类似的图像(用于加载图像的圆圈等)。
在我的视图控制器中,我调用CustomCircleView,它可以很好地创建基圆(我使用这个圆作为背景颜色,这样我就可以单独调整它的色调)。然后我调用我的DialView类,它也可以很好地创建基圆,但是我的裁剪图没有出现在我期望的位置。
我想要的是有12个均匀分布的圆,并使用EOFill使它们透明。EOFill运行正常,所有内容都显示无误,但我的圆圈没有出现在我期望的位置;这可能意味着我的数学有问题。
相关代码如下:
- (void)drawRect:(CGRect)rect
{
int inset = 2; // space between cutouts and edge of dial
int circleDiameter = self.bounds.size.width - inset * 2;
float circleRadius = circleDiameter / 2;
float holeDiameter = circleDiameter * 0.15; // wrong size; is there a formula for this?
float holeRadius = holeDiameter / 2;
int holePadding = 2; // space between cutouts
float hypotenuse = circleRadius - holeRadius - holePadding;
float dispX;
float dispY;
// Set context.
CGContextRef context = UIGraphicsGetCurrentContext();
// Set line stroke parameters.
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor);
CGContextSetFillColorWithColor(context, self.dialColor.CGColor);
// main dial circle; this will remain visible
CGRect ellipse = CGRectMake(self.bounds.origin.x + inset, self.bounds.origin.y + inset, circleDiameter, circleDiameter);
CGContextAddEllipseInRect(context, ellipse);
// grid for debugging; remove once circles line up properly
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context, self.bounds.size.width, self.bounds.size.height);
CGContextMoveToPoint(context, 0, self.bounds.size.height);
CGContextAddLineToPoint(context, self.bounds.size.width, 0);
CGContextStrokePath(context);
// circle spacing based on 12 circles
// hole diameter should be proportional to that of dial diameter
// 1
dispX = (self.bounds.size.width / 2) + (cosf(45) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) - (sinf(45) * hypotenuse) - (holeRadius);
CGRect hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 2
dispX = (self.bounds.size.width / 2) + (cosf(75) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) - (sinf(75) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 3
dispX = (self.bounds.size.width / 2) - (cosf(75) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) - (sinf(75) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 4
dispX = (self.bounds.size.width / 2) - (cosf(45) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) - (sinf(45) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 5
dispX = (self.bounds.size.width / 2) - (cosf(15) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) - (sinf(15) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 6
dispX = (self.bounds.size.width / 2) - (cosf(15) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) + (sinf(15) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 7
dispX = (self.bounds.size.width / 2) - (cosf(45) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) + (sinf(45) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 8
dispX = (self.bounds.size.width / 2) - (cosf(75) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) + (sinf(75) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 9
dispX = (self.bounds.size.width / 2) + (cosf(75) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) + (sinf(75) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
// 0
dispX = (self.bounds.size.width / 2) + (cosf(45) * hypotenuse) - (holeRadius);
dispY = (self.bounds.size.height / 2) + (sinf(45) * hypotenuse) - (holeRadius);
hole = CGRectMake(dispX, dispY, holeDiameter, holeDiameter);
CGContextAddEllipseInRect(context, hole);
CGContextEOFillPath(context);
}发布于 2011-05-09 01:46:13
我想通了。我在trig函数中使用度数,而不是弧度。
如果其他人也在做和我一样的事情,请到Steiner chains中寻找半径比的帮助。
https://stackoverflow.com/questions/5901895
复制相似问题