我的做法只是構建了2個圓弧和填充之間的區域。棘手的是找出這些弧的中心和半徑。看起來相當不錯,只要厚度不太大。 (剪切並粘貼,如果它滿足您的需求,請自行決定。)可以通過使用剪切路徑來改進。
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
// As appropriate for iOS, the code below assumes a coordinate system with
// the x-axis pointing to the right and the y-axis pointing down (flipped from the standard Cartesian convention).
// Therefore, 0 degrees = East, 90 degrees = South, 180 degrees = West,
// -90 degrees = 270 degrees = North (once again, flipped from the standard Cartesian convention).
CGFloat startingAngle = 90.0; // South
CGFloat endingAngle = -45.0; // North-East
BOOL weGoFromTheStartingAngleToTheEndingAngleInACounterClockwiseDirection = YES; // change this to NO if necessary
CGFloat startingThickness = 2.0;
CGFloat endingThickness = 12.0;
CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
CGFloat meanRadius = 0.9 * fminf(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
// the parameters above should be supplied by the user
// the parameters below are derived from the parameters supplied above
CGFloat deltaAngle = fabsf(endingAngle - startingAngle);
// projectedEndingThickness is the ending thickness we would have if the two arcs
// subtended an angle of 180 degrees at their respective centers instead of deltaAngle
CGFloat projectedEndingThickness = startingThickness + (endingThickness - startingThickness) * (180.0/deltaAngle);
CGFloat centerOffset = (projectedEndingThickness - startingThickness)/4.0;
CGPoint centerForInnerArc = CGPointMake(center.x + centerOffset * cos(startingAngle * M_PI/180.0),
center.y + centerOffset * sin(startingAngle * M_PI/180.0));
CGPoint centerForOuterArc = CGPointMake(center.x - centerOffset * cos(startingAngle * M_PI/180.0),
center.y - centerOffset * sin(startingAngle * M_PI/180.0));
CGFloat radiusForInnerArc = meanRadius - (startingThickness + projectedEndingThickness)/4.0;
CGFloat radiusForOuterArc = meanRadius + (startingThickness + projectedEndingThickness)/4.0;
CGPathAddArc(path,
NULL,
centerForInnerArc.x,
centerForInnerArc.y,
radiusForInnerArc,
endingAngle * (M_PI/180.0),
startingAngle * (M_PI/180.0),
!weGoFromTheStartingAngleToTheEndingAngleInACounterClockwiseDirection
);
CGPathAddArc(path,
NULL,
centerForOuterArc.x,
centerForOuterArc.y,
radiusForOuterArc,
startingAngle * (M_PI/180.0),
endingAngle * (M_PI/180.0),
weGoFromTheStartingAngleToTheEndingAngleInACounterClockwiseDirection
);
CGContextAddPath(context, path);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(context);
CGPathRelease(path);
}
這實際上看起來非常棒!你爲我節省了不少工作。這比我工作的方法簡單得多(求解螺旋的貝塞爾多項式方程)。我得到它的工作90°的倍數,但任意角度將是一個痛苦。這是好得多... – 2012-07-08 08:00:12
@JonHull很高興你喜歡它。我剛剛意識到我已經隱含地認爲'endingThickness> = startingThickness',但你應該能夠很容易地安排你的輸入參數,以便滿足這個條件。如果不是這樣,那麼可能會出現「預計的結果厚度」爲負值的情況,然後我再也不能確定代數了。它可能仍然有效,但我沒有測試過它。 – inwit 2012-07-08 14:29:55
哦偉大的工作兄弟,,,,你是一個真正的救星,,,謝謝 – Dhiru 2017-06-19 11:23:58