Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIView阴影使用BezierPath大量偏移阴影_Ios_Swift_Uiview_Shadow - Fatal编程技术网

Ios UIView阴影使用BezierPath大量偏移阴影

Ios UIView阴影使用BezierPath大量偏移阴影,ios,swift,uiview,shadow,Ios,Swift,Uiview,Shadow,我已将阴影添加到UIView,但得到以下结果: 代码如下: 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.bl

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

代码如下:

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
考虑到我给阴影路径提供了与蓝色视图(主视图)完全相同的帧,我不明白为什么阴影会如此偏移。我知道我可以使用shadowOffset属性来解决这个问题,但是我尝试使用shadowPath的全部原因是不使用shadowOffset,因为它在规模上可能存在一些性能问题

更新:将mainView.frame固定到mainView.bounds后,阴影将正确对齐。但是,阴影在主视图的顶部似乎仍有轻微偏移(上面有较强的阴影):


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

mainView.layer.shadowPath = UIBezierPath(roundedRect: mainView.bounds, cornerRadius: 8).cgPath
换句话说,您需要一个原点为
(0,0)
的矩形,而不是位置为
mainView

阴影在主视图的顶部似乎仍有轻微偏移

这是因为
shadowOffset
有一个默认值
(0.0,-3.0)

为了避免这种情况,您可以使用
offsetBy

mainView.layer.shadowPath=UIBezierPath(roundedRect:mainView.bounds.offsetBy(dx:0.0,dy:3.0),cornerRadius:8)。cgPath

或者只需将
.zero
传递到
阴影偏移量


mainView.layer.shadowOffset=.zero

尝试将mainView.frame更改为mainView.bounds未意识到它是根据视图坐标指定的!谢谢。修复后,你知道为什么UIView上方的阴影比下方的阴影更强烈吗(用截图更新了我的原始问题)?我本以为阴影会均匀分布。@pizza7我唯一的想法是你的视野高度增加了。当大小更改时,是否正在更新阴影?例如,在
布局子视图中