gpt4 book ai didi

ios - 仅从 UIView 的 3 个边绘制阴影

转载 作者:IT王子 更新时间:2023-10-29 08:02:16 26 4
gpt4 key购买 nike

我已经成功地实现了在我的 UIView 周围绘制阴影,如下所示:

block1.layer.masksToBounds = NO;
block1.layer.shadowOffset = CGSizeMake(0, 0);
block1.layer.shadowRadius = 1;
block1.layer.shadowOpacity = 0.7;

现在发生的是我有一个矩形 UIView,我想在它的三个边上绘制阴影,留下它的 bottom没有 影子。

我知道我必须通过创建一个新的 UIBezierPath 来指定 block1.layer.shadowPath,但我不确定该怎么做。

显然,设置 layer.shadowOffset 对我来说不起作用。

提前致谢!

最佳答案

我知道你说设置 layer.shadowOffset 对你不起作用,但你可以输入负值,所以设置它 layer.shadowOffset = CGSizeMake(0.0, -2.0 ) 会接近您正在寻找的效果,但我当然希望您希望它在三个方面是均匀的。

所以这里我们使用layer.shadowPath!

UIView *block1 = [[UIView alloc] initWithFrame:CGRectMake(32.0, 32.0, 128.0, 128.0)];
[block1 setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:block1];

block1.layer.masksToBounds = NO;
block1.layer.shadowOffset = CGSizeMake(0, 0);
block1.layer.shadowRadius = 1;
block1.layer.shadowOpacity = 0.7;

UIBezierPath *path = [UIBezierPath bezierPath];

// Start at the Top Left Corner
[path moveToPoint:CGPointMake(0.0, 0.0)];

// Move to the Top Right Corner
[path addLineToPoint:CGPointMake(CGRectGetWidth(block1.frame), 0.0)];

// Move to the Bottom Right Corner
[path addLineToPoint:CGPointMake(CGRectGetWidth(block1.frame), CGRectGetHeight(block1.frame))];

// This is the extra point in the middle :) Its the secret sauce.
[path addLineToPoint:CGPointMake(CGRectGetWidth(block1.frame) / 2.0, CGRectGetHeight(block1.frame) / 2.0)];

// Move to the Bottom Left Corner
[path addLineToPoint:CGPointMake(0.0, CGRectGetHeight(block1.frame))];

// Move to the Close the Path
[path closePath];

block1.layer.shadowPath = path.CGPath;

为了让您了解发生了什么,这里是您刚刚绘制的实际阴影路径:)

enter image description here

可以在其他行之前或之后移动额外的中间点来选择将省略哪一侧。

关于ios - 仅从 UIView 的 3 个边绘制阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15704163/

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