【问题标题】:Drawing arc and rectangle in CALayer in Objective-C在Objective-C的CALayer中绘制圆弧和矩形
【发布时间】:2012-06-02 00:23:40
【问题描述】:

我在向用户呈现弧形然后在同一个 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); 
   }

【问题讨论】:

    标签: objective-c ios calayer cgcontext


    【解决方案1】:

    这里的问题是你画的是一个圆弧然后是一个填充的矩形 所以弧被绘制在这个填充的矩形后面

    一个快速的解决方法是在绘制矩形之后移动圆弧的绘制 绘制矩形后移动Arc的绘制

    【讨论】:

    • 太好了,你知道为什么吗?我的意思是,背后的逻辑是什么?
    • 好吧,我猜你画的矩形被填满了,所以它重叠并覆盖了旧的弧线:)
    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多