我想画一 strip 阴影的线,但我不想保留线,只保留阴影。
我试图将线条的描边颜色设置为清除,但当我这样做时阴影也消失了。
下面的代码创建了 2 条线,我只想保留阴影,因为它看起来更好,而且不像线那样像素化。
这可能吗?
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef shadowColor = CGColorCreate(colorSpace, components);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(10,10), 4.0, shadowColor);
谢谢。
最佳答案
所以你不能直接这样做,因为阴影反射(reflect)了路径,所以如果你让路径透明,那么阴影也会透明。我可以想到一些解决方法(具体取决于您在做什么),但一种偷偷摸摸的方法就是将阴影绘制在路径下方足够远的地方,您可以直接在它上面绘制,基本上将其删除。例如,如果背景是白色,这将完成您想要的:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef shadowColor = CGColorCreate(colorSpace, components);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f,20.0f), 4.0, shadowColor);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
}
希望这对您有所帮助。
关于iphone - 用阴影画线,但只想保留阴影。操作系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7354327/