iOS Core Graphics: Draw ONLY shadows of a CGPath

后端 未结 1 1244
刺人心
刺人心 2020-12-07 23:30

I am drawing a simple path in iOS 5 with Core Graphics:

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(   path, NULL, center.x   , topMargi         


        
相关标签:
1条回答
  • 2020-12-07 23:43

    I suggest you set the context's clipping path to the inverse of the shape's path, configure the shadow, and fill the shape normally, with full opacity. The clipping path will mask out the fill color, and only the shadow would remain.

    CGContextSaveGState(context);
    CGRect boundingRect = CGContextGetClipBoundingBox(context);
    CGContextAddRect(context, boundingRect);
    CGContextAddPath(context, path);
    CGContextEOClip(context);
    
    [[UIColor blackColor] setFill];
    CGContextAddPath(context, path);
    CGContextSetShadowWithColor(context, CGSizeMake(1, 1), 1.0, highlightColor);
    CGContextSetBlendMode (context, kCGBlendModeNormal);
    CGContextFillPath(context);
    
    CGContextAddPath(context, path);
    CGContextSetShadowWithColor(context, CGSizeMake(-1, -1), 1.0, shadowColor);
    CGContextSetBlendMode (context, kCGBlendModeNormal);
    CGContextFillPath(context);
    
    CGContextRestoreGState(context);
    

    The trick is using CGContextEOClip and an additional rectangle subpath to set the clipping area to whatever is not covered by the original path. This will work for any path that is not self-intersecting.

    0 讨论(0)
提交回复
热议问题