gpt4 book ai didi

ios - UIView Shadow 使用 BezierPath 大量偏移阴影

转载 作者:行者123 更新时间:2023-11-28 15:16:18 25 4
gpt4 key购买 nike

我已经向 UIView 添加了阴影,但得到以下结果:

enter image description here

代码如下:

mainView.layer.cornerRadius = 8
mainView.layer.shadowColor = UIColor.black.cgColor
mainView.layer.shadowOpacity = 0.2
mainView.layer.shadowRadius = 10
mainView.clipsToBounds = false
mainView.backgroundColor = UIColor.blue
mainView.layer.shadowPath = UIBezierPath(roundedRect: mainView.frame, cornerRadius: 8).cgPath

鉴于我为 shadowPath 提供了与蓝色 View (mainView) 相同的框架,我不明白为什么阴影偏移如此之大。我知道我可以使用 shadowOffset 属性修复此问题,但我尝试使用 shadowPath 的全部原因是使用 shadowOffset,因为它可能存在一些大规模的性能问题。

更新:将 mainView.frame 固定为 mainView.bounds 后,阴影已适当对齐。然而,阴影似乎仍然在 mainView 的顶部略有偏移(上面有更强的阴影):

enter image description here

最佳答案

请注意阴影是在 View 的坐标中指定的,因此您应该使用 mainView.bounds

mainView.layer.shadowPath = UIBezierPath(roundedRect: mainView.bounds, cornerRadius: 8).cgPath

换句话说,您想要一个原点为 (0, 0) 的矩形,而不是 mainView 的位置。

关于ios - UIView Shadow 使用 BezierPath 大量偏移阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46797761/

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