OGeek|极客世界-中国程序员成长平台

标题: ios - Core Graphics - 如何在不画线的情况下绘制阴影? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:00
标题: ios - Core Graphics - 如何在不画线的情况下绘制阴影?

看看这段代码:

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



Best Answer-推荐答案


我能够使用描边颜色 white 和混合模式 multiply 来实现这一点:

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

它只画阴影,没有描边。

关于ios - Core Graphics - 如何在不画线的情况下绘制阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22688948/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4