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

问题描述 投票:2回答:2

看看这段代码:

- (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的类似问题的答案对我没有帮助

ios objective-c core-graphics
2个回答
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
投票

我能够使用笔触颜色白色实现这一点,并且混合模式相乘:

// ...
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()
// ...

它只绘制阴影,没有中风。

© www.soinside.com 2019 - 2024. All rights reserved.