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);
}
截至4月9日,新用戶必須等待8小時才能自行回覆。請參閱http://meta.stackexchange.com/questions/86185和http://meta.stackexchange.com/questions/59445 - 如果可以,請回答問題,使其不再顯示爲未答覆。 – 2011-05-05 19:47:47