核心图形 - 如何绘制阴影而不画线?

时间:2014-03-27 13:20:53

标签: ios objective-c core-graphics

看看这段代码:

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

  CGContextRef context = UIGraphicsGetCurrentContext();

  CGSize  myShadowOffset = CGSizeMake (-10,  15);

  CGContextSaveGState(context);

  CGContextSetShadow (context, myShadowOffset, 5);

  CGContextSetLineWidth(context, 4.0);
  CGContextSetStrokeColorWithColor(context,
                                 [UIColor blueColor].CGColor);
  CGRect rectangle = CGRectMake(60,170,200,200);
  CGContextAddEllipseInRect(context, rectangle);
  CGContextStrokePath(context);
  CGContextRestoreGState(context);
}

它的作用是为这个圆圈画一个圆圈和一个阴影。但是,我无法让我的脑海中如何画出阴影而不画圆圈线?我怎么做?关于SO的类似问题的答案对我没有帮助

2 个答案:

答案 0 :(得分:1)

请尝试以下方法:

//If you would like your shadow color to be blue, change it to blue.
CGContextSetShadowWithColor(context, CGSizeMake(-10.0f,  15.0f), 5.0f, [UIColor lightGrayColor].CGColor);
CGContextSetStrokeColorWithColor(context, self.backgroundColor.CGColor);
CGContextStrokePath(context);

我刚刚对它进行了测试;效果似乎是可取的。

希望这有帮助。

答案 1 :(得分:1)

我能够使用笔划颜色白色和混合模式

// ...
context.setStrokeColor(UIColor.white.cgColor)
context.setShadow(offset: CGSize.zero, blur: 8.0, color: UIColor.black.cgColor)
context.setBlendMode(.multiply)
context.addPath(...) <--- ADD YOUR PATH HERE
context.strokePath()
// ...

它仅绘制阴影,而没有描边。

相关问题