![](/img/trans.png)
[英]How to show only corners of CALayer rectangle in macOS Objective-c?
[英]Drawing arc and rectangle in CALayer in Objective-C
我在向用戶展示弧線然后在同一CALayer中顯示矩形時遇到問題。 我成功繪制了兩者,但僅當圓弧相同或在矩形坐標上方繪制時才顯示矩形。
知道我錯過了什么嗎?
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)p_contex
{
CGMutablePathRef thePath = CGPathCreateMutable();
CGPathMoveToPoint(thePath,NULL,mReference.x,mReference.y);
CGPathAddArc(thePath,NULL,
mReference.x, mReference.y,
S_RADIO, lStartAngle, lStopAngle ,
0);
CGPathCloseSubpath(thePath);
CGContextSetRGBFillColor(p_contex, lRed,lGreen,lBlue,lAlpha);
CGContextSetRGBStrokeColor(p_contex, lRed,lGreen,lBlue,lAlpha);
CGContextAddPath(p_contex, thePath );
CGContextSaveGState(p_contex);
CGContextClip(p_contex);
CGContextDrawRadialGradient(p_contex,
[self buildGradientColor],
mReference , 5, mReference, S_RADIO, 0);
CGContextSaveGState(p_contex);
// release the path
CFRelease(thePath);
CGGradientRelease(mGradient);
CGMutablePathRef retPath = CGPathCreateMutable();
CGRect rect = CGRectMake(0,300, 200, 40);
float radius = 10;
CGRect innerRect = CGRectInset(rect, radius, radius);
CGFloat inside_right = innerRect.origin.x + innerRect.size.width;
CGFloat outside_right = rect.origin.x + rect.size.width;
CGFloat inside_bottom = innerRect.origin.y + innerRect.size.height;
CGFloat outside_bottom = rect.origin.y + rect.size.height;
CGFloat inside_top = innerRect.origin.y;
CGFloat outside_top = rect.origin.y;
CGFloat outside_left = rect.origin.x;
CGPathMoveToPoint(retPath, NULL, innerRect.origin.x, outside_top);
CGPathAddLineToPoint(retPath, NULL, inside_right, outside_top);
CGPathAddArcToPoint(retPath, NULL, outside_right, outside_top, outside_right, inside_top, radius);
CGPathAddLineToPoint(retPath, NULL, outside_right, inside_bottom);
CGPathAddArcToPoint(retPath, NULL, outside_right, outside_bottom, inside_right, outside_bottom, radius);
CGPathAddLineToPoint(retPath, NULL, innerRect.origin.x, outside_bottom);
CGPathAddArcToPoint(retPath, NULL, outside_left, outside_bottom, outside_left, inside_bottom, radius);
CGPathAddLineToPoint(retPath, NULL, outside_left, inside_top);
CGPathAddArcToPoint(retPath, NULL, outside_left, outside_top, innerRect.origin.x, outside_top, radius);
CGPathCloseSubpath(retPath);
CGContextAddPath(p_contex, retPath );
CGContextFillPath(p_contex);
}
這里的問題是,先繪制圓弧,然后繪制實心矩形,這樣弧形就會在此實心矩形后面繪制
一個快速的解決方法是在繪制矩形之后移動弧形的圖形在繪制矩形之后移動弧形的圖形
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.