UIBezierPath with color gradient

前端 未结 5 1655
孤街浪徒
孤街浪徒 2021-02-08 15:21

I\'ve got an question about UIBezierPath.

For example I\'ve got this path:

\"\"

Now I want to have

5条回答
  •  余生分开走
    2021-02-08 15:41

    You can try this:

        //// General Declarations
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    //// Shadow Declarations
    NSShadow* shadow = [[NSShadow alloc] init];
    [shadow setShadowColor: UIColor.whiteColor];
    [shadow setShadowOffset: CGSizeMake(2.1, -4.1)];
    [shadow setShadowBlurRadius: 5];
    
    //// Bezier Drawing
    UIBezierPath* bezierPath = UIBezierPath.bezierPath;
    [UIColor.blackColor setStroke];
    bezierPath.lineWidth = 1;
    [bezierPath stroke];
    
    
    //// Bezier 2 Drawing
    UIBezierPath* bezier2Path = UIBezierPath.bezierPath;
    [bezier2Path moveToPoint: CGPointMake(170.5, 59.5)];
    [bezier2Path addCurveToPoint: CGPointMake(170.5, 71.5) controlPoint1: CGPointMake(173.5, 65.5) controlPoint2: CGPointMake(170.5, 71.5)];
    [bezier2Path addLineToPoint: CGPointMake(155.5, 57.5)];
    [bezier2Path addLineToPoint: CGPointMake(170.5, 59.5)];
    [UIColor.redColor setFill];
    [bezier2Path fill];
    
    ////// Bezier 2 Inner Shadow
    CGContextSaveGState(context);
    UIRectClip(bezier2Path.bounds);
    CGContextSetShadowWithColor(context, CGSizeZero, 0, NULL);
    
    CGContextSetAlpha(context, CGColorGetAlpha([shadow.shadowColor CGColor]));
    CGContextBeginTransparencyLayer(context, NULL);
    {
        UIColor* opaqueShadow = [shadow.shadowColor colorWithAlphaComponent: 1];
        CGContextSetShadowWithColor(context, shadow.shadowOffset, shadow.shadowBlurRadius, [opaqueShadow CGColor]);
        CGContextSetBlendMode(context, kCGBlendModeSourceOut);
        CGContextBeginTransparencyLayer(context, NULL);
    
        [opaqueShadow setFill];
        [bezier2Path fill];
    
        CGContextEndTransparencyLayer(context);
    }
    CGContextEndTransparencyLayer(context);
    CGContextRestoreGState(context);
    

    and the result will be:

提交回复
热议问题