在drawRect 中定義了一個圓,下面的代碼是用來定義圓的弧度。
CGFloat width = rect.size.width-rect.origin.x;
CGFloat height = rect.size.height-rect.origin.y;
CGFloat xPos = rect.origin.x;
CGFloat yPos = rect.origin.y;
CGFloat arcStake = (width * 2) * 0.25;
CGFloat radius = height/2;
CGPoint centre = CGPointMake(xPos+width/2, yPos+height/2);
CGFloat angle = acos(arcStake/(2*radius));
CGFloat startAng = radians(180) + angle;
CGFloat endAng = radians(360) - angle;
// Define 2 CGPoints of arc
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, NULL, centre.x, centre.y, radius, startAng, endAng, 0);
CGPathAddLineToPoint(path, NULL, xPos+width/2, yPos+height/2);
CGPathCloseSubpath(path);
定義兩個CGPoints 的弧度,如下圖的效果:
在半徑為r的圓中,角度為a的點數有下面的坐標:
其中a是從圓的最右側點開始測量。
x = r*cos(a) + center.x
y = r*sin(a) + center.y