ios - UIBezierPath的影子: how to hide the stroke's shadow?

标签 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

取消注释[路径笔划]给了我这个。我想要描边,但不想看到它在形状后面。

enter image description here

最佳答案

我怀疑不是 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

关于ios - UIBezierPath的影子: how to hide the stroke's shadow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20456824/

相关文章:

iOS 应用程序 - iOS 11.4 随机崩溃

ios - 修复 Swift simd 库中的错误(以及缺少文档?)

android - Lollipop : Had trouble showing a shadow under the overflow menu

Android 无法去除顶部和底部的颜色

ios - 更改背景中阴影的颜色

ios - Xcode、 swift : How to center popover box

ios - 如果未附加调试器,则 React Native 应用程序在启动时无响应

ios - 在 iOS 中确定闭合轮廓的算法

core-graphics - 缩放 UIBezierPath 使其移动

ios - 在 Core Graphics 中创建一个带有多色线段的圆