【问题标题】:draw a shadow around the shape,core graphic在形状周围绘制阴影,核心图形
【发布时间】:2012-11-08 20:30:43
【问题描述】:

我正在通过以下操作绘制一个带有笔划的形状

- (void)drawRect:(CGRect)rect
{
    // Draw a cross rectagle
    CGContextRef    context     =   UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);

    CGContextBeginPath(context);
    CGContextMoveToPoint    (context, 190, 0);
    CGContextAddLineToPoint (context, 220, 0);
    CGContextAddLineToPoint (context, 300, 80);
    CGContextAddLineToPoint (context, 300, 110);
    CGContextClosePath(context);

    CGContextSetFillColorWithColor(context, bgColor);                           // fill color
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);     // set color for stroke
    CGContextSetLineWidth(context, .8);                                         // set width for stroke
    CGContextDrawPath(context,  kCGPathFillStroke);                             // do fill and stroke together


    CGContextEOClip(context);
    CGContextSetShadowWithColor(context, CGSizeMake(1, 1), 1.0, [UIColor whiteColor].CGColor);
    CGContextSetBlendMode (context, kCGBlendModeScreen);
    CGContextRestoreGState(context);
}

我的结局如下(十字旗)

现在,我也想在十字旗周围投下一些阴影。

我应该怎么做才能做到这一点。请就这个问题给我建议。谢谢。

【问题讨论】:

    标签: objective-c ios core-graphics drawrect


    【解决方案1】:

    CGContextSetShadowCGContextSetShadowWithColor (documentation 1, documentation 2)

    在你的情况下,我能够通过

    获得阴影
    ...
    CGContextSaveGState(context);
    
    CGContextSetShadowWithColor(context, CGSizeMake(-3 , 2), 4.0, [UIColor whiteColor].CGColor);
    
    CGContextBeginPath(context);
    CGContextMoveToPoint    (context, 190, 0);
    ...
    

    我从底部删除了这些(剪辑在这里没有做任何事情,为什么是混合模式?)

    CGContextEOClip(context);
    CGContextSetShadowWithColor(context, CGSizeMake(1, 1), 1.0, [UIColor whiteColor].CGColor);
    CGContextSetBlendMode (context, kCGBlendModeScreen);
    

    【讨论】:

      【解决方案2】:

      您应该能够通过使用 UIBezierPath 跟踪您的标志并将阴影应用到路径来完成此操作。

      这里有一些可能有用的示例代码

      // create highlight
      UIRectCorner corners = UIRectCornerTopLeft;
      UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height + 50.0f) byRoundingCorners:corners cornerRadii:CGSizeMake(32.0f, 32.0f)];
      [[self layer] setShadowPath:[shadowPath CGPath]];
      [[self layer] setShadowOpacity:0.5f];
      [[self layer] setShadowRadius:25.0f];
      [[self layer] setShadowOffset:CGSizeMake(0.0f, 0.0f)];
      [[self layer] setShadowColor:[[UIColor colorWithRed:1.0f green:1.0f blue:0.75f alpha:1.0f] CGColor]];
      

      【讨论】:

      • 这有点大材小用了,如果你已经在使用 CG 来绘制你的路径,你还不如使用 CG 来绘制阴影作为其中的一部分。
      • @propstm:我不知道它是否有效。但是,当使用 UIBeizerPath 时,我们应该提供一个应用阴影的矩形。在这种情况下,我使用CGContextAddLineToPoint 绘制十字旗。如果我错了,请纠正我。
      • @ttran:UIBezierPath 不仅适用于矩形。您可以使用与问题中的 CGContext 路径代码类似的代码创建 UIBezierPath,并且生成的路径和阴影将是相同的。
      猜你喜欢
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 2013-10-17
      • 2017-03-13
      • 1970-01-01
      相关资源
      最近更新 更多