UIBezierPath的影子:如何隐藏笔画的阴影?

时间:2013-12-08 17:54:50

标签: ios shadow uibezierpath stroke

我正在尝试在iOS7中绘制UIBezierPathShape然后应用阴影。除了当我描绘路径时,笔划显示在形状后面,这效果很好。我怎么能纠正这个?

代码:

- (void)drawDiamondWithCount:(NSUInteger)count inRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    UIGraphicsPushContext(ctx);
    UIEdgeInsets insets = UIEdgeInsetsMake(cardEdgeInsetTop, cardEdgeInsetRight, cardEdgeInsetBottom, cardEdgeInsetLeft);
    CGRect insetsRect = UIEdgeInsetsInsetRect(rect, insets);

    CGFloat shapeHeight = insetsRect.size.height / (double) count;
    CGRect shapeRect;
    for (NSUInteger i = 0; i < count; ++i) {
        // Get the rect for the single shape
        int numRemainingShapes = count - i - 1;
        CGFloat remainingBottomSpace = numRemainingShapes * shapeHeight;
        insets = UIEdgeInsetsMake(i * shapeHeight + shapeEdgeInsets, 0, remainingBottomSpace + shapeEdgeInsets, 0);
        shapeRect = UIEdgeInsetsInsetRect(insetsRect, insets);
        UIBezierPath *path = [self getDiamondPath:shapeRect];
        [[UIColor redColor] setFill];
        [[UIColor blackColor] setStroke];
        UIGraphicsPushContext(ctx);
        CGContextSetShadow(ctx, CGSizeMake(5, 2), 5);
        [path fill];
        UIGraphicsPopContext();
        //[path stroke];
    }
    UIGraphicsPopContext();
}

这给了我想要的东西,减去中风 This gives me what I want, minus the stroke

取消注释[path stroke]给了我这个。我想要中风,但不想在形状后面看到它。

enter image description here

1 个答案:

答案 0 :(得分:2)

我怀疑代替UIGraphicsPushContextUIGraphicsPopContext,我认为您需要CGContextSaveGStateCGContextRestoreGState

// create context and configure

CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor redColor] setFill];
[[UIColor blackColor] setStroke];

// create path

UIBezierPath *path = ...;
path.lineJoinStyle = kCGLineJoinMiter;
path.lineWidth = 2.0;

// fill the center with shadow

CGContextSaveGState(ctx);
CGContextSetShadow(ctx, CGSizeMake(5, 2), 5);
[path fill];
CGContextRestoreGState(ctx);

// stroke border without shadow

CGContextSetLineWidth(ctx, 2.0);
[path stroke];

使用UIGraphicsPushContextUIGraphicsPopContext获得:

line shadow

使用CGContextSaveGStateCGContextRestoreGState获得:

enter image description here