首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >圆内的圆环,动态调整大小

圆内的圆环,动态调整大小
EN

Stack Overflow用户
提问于 2011-05-06 01:37:31
回答 1查看 755关注 0票数 2

全。很抱歉,如果这个问题已经被问过并回答了,我在搜索时找不到这个特定的问题。

我正在尝试创建一个circle类,它动态地在一个圆内创建一个圆环,并且在它背后的trig有问题。这个想法是为旋转电话创建一个图像,但我希望能够重用这个类来处理其他类似的图像(用于加载图像的圆圈等)。

在我的视图控制器中,我调用CustomCircleView,它可以很好地创建基圆(我使用这个圆作为背景颜色,这样我就可以单独调整它的色调)。然后我调用我的DialView类,它也可以很好地创建基圆,但是我的裁剪图没有出现在我期望的位置。

我想要的是有12个均匀分布的圆,并使用EOFill使它们透明。EOFill运行正常,所有内容都显示无误,但我的圆圈没有出现在我期望的位置;这可能意味着我的数学有问题。

相关代码如下:

代码语言:javascript
复制
- (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);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-09 01:46:13

我想通了。我在trig函数中使用度数,而不是弧度。

如果其他人也在做和我一样的事情,请到Steiner chains中寻找半径比的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5901895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档