UIBezierPath和渐变iOS

Unm*_*ful 0 gradient ios uibezierpath

我有值的路径,我想制作这个渐变.

这是代码:

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(context, [[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:171.0/255.0 alpha:0.6] CGColor]);
    CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:171.0/255.0 alpha:0.6] CGColor]);



    UIBezierPath *aPath = [UIBezierPath bezierPath];
    [aPath moveToPoint:CGPointMake(30.0, 100.0)];


    [aPath addLineToPoint:CGPointMake(200.0, 120.0)];
    [aPath addLineToPoint:CGPointMake(300, 210)];
    [aPath addLineToPoint:CGPointMake(300, 420)];
    [aPath addLineToPoint:CGPointMake(30, 420.0)];
    [aPath addLineToPoint:CGPointMake(30, 100.0)];

    [aPath closePath];
    [aPath fill];
Run Code Online (Sandbox Code Playgroud)

有没有指出这个代码的问题?

Inj*_*ios 6

首先 - 我用Bezier路径创建了简单的箭头:

UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(24.5, 1.5)];
[bezierPath addLineToPoint: CGPointMake(2.5, 14.5)];
[bezierPath addLineToPoint: CGPointMake(24.5, 28.5)];
[bezierPath addLineToPoint: CGPointMake(24.5, 1.5)];
[bezierPath closePath];
[[UIColor blackColor] setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

然后我画了从黑到白的简单线性渐变:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();

NSArray* simpleLinearGradientColors = [NSArray arrayWithObjects:
                                       (id)[UIColor blackColor].CGColor,
                                       (id)[UIColor whiteColor].CGColor, nil];
CGFloat simpleLinearGradientLocations[] = {0, 1};
CGGradientRef simpleLinearGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)simpleLinearGradientColors, simpleLinearGradientLocations);


// Bezier Drawing
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(24.5, 1.5)];
[bezierPath addLineToPoint: CGPointMake(2.5, 14.5)];
[bezierPath addLineToPoint: CGPointMake(24.5, 28.5)];
[bezierPath addLineToPoint: CGPointMake(24.5, 1.5)];
[bezierPath closePath];
CGContextSaveGState(context);
[bezierPath addClip];
CGContextDrawLinearGradient(context, simpleLinearGradient, CGPointMake(2.5, 15), CGPointMake(24.5, 15), 0);
CGContextRestoreGState(context);

[[UIColor blackColor] setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];

CGGradientRelease(simpleLinearGradient);
CGColorSpaceRelease(colorSpace);
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

在此输入图像描述

基本上你可以用一堆设置(位置,颜色)创建线性,径向渐变,当然你应该修改上面的代码.