2011-05-05 67 views
2

全部。 对不起,如果这已被问及已回答,我找不到这个具体问题,当我做了我的搜索。圈内圈圈,动态大小

我想创建一个圆形类,它可以动态地在一个圆圈内创建一圈圆环,并且在背后有一个触发问题。这个想法是为旋转手机创建一个图像,但我希望能够将这个类重用于其他类似的图像(加载图像的圆圈等)。

从我的viewcontroller中,我调用了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); 
} 
+2

截至4月9日,新用户必须等待8小时才能自行回复。请参阅http://meta.stackexchange.com/questions/86185和http://meta.stackexchange.com/questions/59445 - 如果可以,请回答问题,使其不再显示为未答复。 – 2011-05-05 19:47:47

回答

0

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

如果有其他人试图做同样的事情,我是,查看斯坦纳链寻求你的半径比的帮助。