CGContextEOFillPath + CGContextDrawLinearGradient

时间:2012-10-04 09:57:42

标签: objective-c ios quartz-2d

我成功地使用CGContextEOFillPath绘制了一个矩形,其内部有一个圆角矩形的形状。我可以改变我的形状的颜色。但是,我想使用渐变,我想我必须使用CGContextDrawLinearGradient,但是,我无法使其工作。它不断绘制整个矩形,包括孔。

以下是我正在使用的代码

- (void)drawRect:(CGRect)rect 
{
[super drawRect:rect];

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGFloat frameWidth = 1; //[CQMFloatingContentOverlayView frameWidth];

CGFloat radius = [self cornerRadius];
CGSize viewSize = [self frame].size;
CGPathRef path = CQMPathCreateRoundingRect(CGRectMake(frameWidth, frameWidth,
                                                      viewSize.width - frameWidth * 2,
                                                      viewSize.height - frameWidth * 2),
                                 radius, radius, radius, radius);
CGContextAddRect(context, CGRectMake(0, 0, viewSize.width, viewSize.height));
CGContextAddPath(context, path);
    CGContextEOFillPath(context);
CGPathRelease(path);
CGContextRestoreGState(context);
}


CGPathRef CQMPathCreateRoundingRect(CGRect rect, CGFloat blRadius, CGFloat brRadius, CGFloat trRadius, CGFloat tlRadius) {
CGPoint tlPoint = rect.origin;
CGPoint brPoint = CGPointMake(rect.origin.x + rect.size.width,
                              rect.origin.y + rect.size.height);
CGMutablePathRef path = CGPathCreateMutable();

CGPathMoveToPoint(path, NULL, tlPoint.x + tlRadius, tlPoint.y);
CGPathAddArcToPoint(path, NULL,
                    brPoint.x, tlPoint.y,
                    brPoint.x, tlPoint.y + trRadius,
                    trRadius);
CGPathAddArcToPoint(path, NULL,
                    brPoint.x, brPoint.y,
                    brPoint.x - brRadius, brPoint.y,
                    brRadius);
CGPathAddArcToPoint(path, NULL,
                    tlPoint.x, brPoint.y,
                    tlPoint.x, brPoint.y - blRadius,
                    blRadius);
CGPathAddArcToPoint(path, NULL,
                    tlPoint.x, tlPoint.y,
                    tlPoint.x + tlRadius, tlPoint.y,
                    tlRadius);
CGPathCloseSubpath(path);

return path;
}

我不知道在哪里放置渐变代码,我已经在CGContextEOFillPath之前和之后尝试但是无法达到我的目标。

1 个答案:

答案 0 :(得分:0)

这段代码应该是你想要的(检查语法和拼写错误,代码直接写在答案中)

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGFloat gradientColors[] =
    {        
        0.1, 0.1, 0.5, 1.00,
        0.9, 0.9, 1.0, 1.00,
    };

    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradientColors, NULL, sizeof(gradientColors)/(sizeof(gradientColors[0])*4));

    CGColorSpaceRelease(colorSpace);

    CGContextSaveGState(context);
    CGFloat frameWidth = 1; //[CQMFloatingContentOverlayView frameWidth];

    CGFloat radius = [self cornerRadius];
    CGSize viewSize = [self frame].size;
    CGPathRef path = CQMPathCreateRoundingRect(CGRectMake(frameWidth, frameWidth,
                                                      viewSize.width - frameWidth * 2,
                                                      viewSize.height - frameWidth * 2),
                                     radius, radius, radius, radius);
    CGContextAddRect(context, CGRectMake(0, 0, viewSize.width, viewSize.height));
    CGContextAddPath(context, path);
    CGContextEOClip(context);

    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0),           CGPointMake(viewSize.width, viewSize.height), kCGGradientDrawsBeforeStartLocation);


CGPathRelease(path);
    CGContextRestoreGState(context);
}