gpt4 book ai didi

iphone - UIView层的内阴影效果?

转载 作者:行者123 更新时间:2023-12-03 18:08:32 31 4
gpt4 key购买 nike

我有以下 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对象之上添加图层,因为它应该是一些按钮后面的一个栏,所以我不知道该怎么办?

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

enter image description here

感谢帮助...

最佳答案

对于其他想知道如何按照 Costique 的建议使用 Core Graphics 绘制内部阴影的人,那么方法如下:(在 iOS 上根据需要进行调整)

在你的drawRect:方法中...

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);

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

  1. 创建您的路径
  2. 设置所需的填充颜色,将此路径添加到上下文中,并填充上下文
  3. 现在创建一个更大的矩形来限制可见路径。在关闭此路径之前,添加可见路径。然后关闭路径,以便创建一个形状并从中减去可见路径。您可能需要研究填充方法(偶数/奇数的非零缠绕),具体取决于您创建这些路径的方式。本质上,为了在将子路径加在一起时“减去”子路径,您需要以相反的方向(一个顺时针方向,另一个逆时针方向)绘制它们(或者更确切地说构造它们)。
  4. 然后,您需要将可见路径设置为上下文中的剪切路径,这样您就不会将其之外的任何内容绘制到屏幕上。
  5. 然后设置上下文上的阴影,包括偏移、模糊和颜色。
  6. 然后用其中的孔填充大形状。颜色并不重要,因为如果你做的一切都正确,你就不会看到这种颜色,而只会看到阴影。

关于iphone - UIView层的内阴影效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4431292/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com