UIView图层的内阴影效果? [英] Inner shadow effect on UIView layer?

查看:107
本文介绍了UIView图层的内阴影效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下CALayer:

I have the following CALayer:

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake(8, 57, 296, 30);
gradient.cornerRadius = 3.0f;
gradient.colors = [NSArray arrayWithObjects:(id)[RGB(130, 0, 140) CGColor], (id)[RGB(108, 0, 120) CGColor], nil];
[self.layer insertSublayer:gradient atIndex:0];

我想为它添加内部阴影效果,但是我不太清楚如何做到这一点。我想我需要在drawRect中绘制,但是这会在其他UIView对象的顶部添加图层,因为它应该是一些按钮背后的条形图,所以我不知道该怎么做?

I'd like to add an inner shadow effect to it, but I am not quite sure how to do this. I suppose I would be required to draw in drawRect, however this would add the layer on top of other UIView objects, since it's supposed to be a bar behind some buttons, so I am at a loss as to what to do?

我可以添加另一个图层,但又不知道如何实现内部阴影效果(如下所示:

I could add another layer, but again, not sure how to achieve the inner shadow effect (like this:

< img src =https://i.stack.imgur.com/MTFCR.pngalt =在此输入图像说明>

帮助赞赏...

推荐答案

如果其他人想知道如何根据Costique建议使用Core Graphics绘制内部阴影,那么这就是:(在iOS上)根据需要调整)

For anyone else wondering how to draw an inner shadow using Core Graphics as per Costique suggestion, then this is how: (on iOS adjust as needed)

在drawRect:方法中...

In your drawRect: method...

CGRect bounds = [self bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat radius = 0.5f * CGRectGetHeight(bounds);


// Create the "visible" path, which will be the shape that gets the inner shadow
// In this case it's just a rounded rect, but could be as complex as your want
CGMutablePathRef visiblePath = CGPathCreateMutable();
CGRect innerRect = CGRectInset(bounds, radius, radius);
CGPathMoveToPoint(visiblePath, NULL, innerRect.origin.x, bounds.origin.y);
CGPathAddLineToPoint(visiblePath, NULL, innerRect.origin.x + innerRect.size.width, bounds.origin.y);
CGPathAddArcToPoint(visiblePath, NULL, bounds.origin.x + bounds.size.width, bounds.origin.y, bounds.origin.x + bounds.size.width, innerRect.origin.y, radius);
CGPathAddLineToPoint(visiblePath, NULL, bounds.origin.x + bounds.size.width, innerRect.origin.y + innerRect.size.height);
CGPathAddArcToPoint(visiblePath, NULL,  bounds.origin.x + bounds.size.width, bounds.origin.y + bounds.size.height, innerRect.origin.x + innerRect.size.width, bounds.origin.y + bounds.size.height, radius);
CGPathAddLineToPoint(visiblePath, NULL, innerRect.origin.x, bounds.origin.y + bounds.size.height);
CGPathAddArcToPoint(visiblePath, NULL,  bounds.origin.x, bounds.origin.y + bounds.size.height, bounds.origin.x, innerRect.origin.y + innerRect.size.height, radius);
CGPathAddLineToPoint(visiblePath, NULL, bounds.origin.x, innerRect.origin.y);
CGPathAddArcToPoint(visiblePath, NULL,  bounds.origin.x, bounds.origin.y, innerRect.origin.x, bounds.origin.y, radius);
CGPathCloseSubpath(visiblePath);

// Fill this path
UIColor *aColor = [UIColor redColor];
[aColor setFill];
CGContextAddPath(context, visiblePath);
CGContextFillPath(context);


// Now create a larger rectangle, which we're going to subtract the visible path from
// and apply a shadow
CGMutablePathRef path = CGPathCreateMutable();
//(when drawing the shadow for a path whichs bounding box is not known pass "CGPathGetPathBoundingBox(visiblePath)" instead of "bounds" in the following line:)
//-42 cuould just be any offset > 0
CGPathAddRect(path, NULL, CGRectInset(bounds, -42, -42));

// Add the visible path (so that it gets subtracted for the shadow)
CGPathAddPath(path, NULL, visiblePath);
CGPathCloseSubpath(path);

// Add the visible paths as the clipping path to the context
CGContextAddPath(context, visiblePath); 
CGContextClip(context);         


// Now setup the shadow properties on the context
aColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 1.0f), 3.0f, [aColor CGColor]);   

// Now fill the rectangle, so the shadow gets drawn
[aColor setFill];   
CGContextSaveGState(context);   
CGContextAddPath(context, path);
CGContextEOFillPath(context);

// Release the paths
CGPathRelease(path);    
CGPathRelease(visiblePath);

因此,基本上有以下步骤:

So, essentially there are the following steps:


  1. 创建路径

  2. 设置所需的填充颜色,将此路径添加到上下文中,并填写上下文

  3. 现在创建一个可以绑定可见路径的较大矩形。在关闭此路径之前,请添加可见路径。然后关闭路径,以便创建一个从中减去可见路径的形状。您可能想要研究填充方法(偶数/奇数的非零绕组),具体取决于您创建这些路径的方式。本质上,为了让子路径在你将它们加在一起时减去,你需要在相反的方向上绘制它们(或者更确切地说是构造它们),一个是顺时针方向,另一个是逆时针方向。

  4. 然后你需要将你的可见路径设置为上下文中的剪切路径,这样你就不会在屏幕外面绘制任何东西了。

  5. 然后在上面设置阴影。上下文,包括偏移,模糊和颜色。

  6. 然后填充其中有孔的大形状。颜色无关紧要,因为如果你做的一切都正确,你就不会看到这种颜色,只看到阴影。

  1. Create your path
  2. Set the fill color you want, add this path to the context, and fill the context
  3. Now create a larger rectangle that can bound the visible path. Before closing this path, add the visible path. Then close the path, so that you create a shape with the visible path subtracted from it. You might want to investigate the fill methods (non-zero winding of even/odd) depending on how you created these paths. In essence, to get the subpaths to "subtract" when you add them together, you need to draw them (or rather construct them) in opposite directions, one clockwise and the other anti-clockwise.
  4. Then you need to set your visible path as the clipping path on the context, so that you don't draw anything outside it to the screen.
  5. Then setup up the shadow on the context, which includes the offset, blur and color.
  6. Then fill the big shape with the hole in it. The color doesn't matter, because if you've done everything right, you won't see this color, just the shadow.

这篇关于UIView图层的内阴影效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆