繁体   English   中英

如何在iOS中打起阴影?

[英]How to round a shadow in iOS?

我创建了一个圆形的UIView,它也应该有一个阴影。 到目前为止,一切正常,但在拐角处,阴影未正确取整。

如何绕过阴影?

这是代码和屏幕截图:

popupView.layer.cornerRadius = 15

popupView.layer.shadowColor = UIColor.black.cgColor
popupView.layer.shadowOffset = CGSize(width: 0, height: 0)
popupView.layer.shadowOpacity = 0.3
popupView.layer.shadowRadius = 3.0
popupView.layer.shadowPath = UIBezierPath(rect: popupView.bounds).cgPath
popupView.layer.shouldRasterize = false

在此处输入图片说明

因此,我基本上将您的代码放入Playground中并进行了一些测试,发现我基本上可以删除view.layer.shadowPath ,并且可以正常工作...

基本上有效

import UIKit

let view = UIView(frame: CGRect(x: 100, y: 100, width: 500, height: 500))
view.backgroundColor = UIColor.white
view.layer.cornerRadius = 15

view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize(width: 0, height: 0)
view.layer.shadowOpacity = 1.0
view.layer.shadowRadius = 6.0
//view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
view.layer.shouldRasterize = false


let outter = UIView(frame: CGRect(x: 0, y: 0, width: 700, height: 700))
outter.backgroundColor = UIColor.red
outter.addSubview(view)


outter

我还发现是:

  • view.layer.masksToBounds = true没有达到预期的结果
  • view.layer.shadowPath = UIBezierPath(roundedRect: view.bounds, cornerRadius: 15).cgPath也可以使用,但是增加了一个故障点,因为如果您在视图上进行更改,则需要记住设置cornerRadius属性,或添加一个变量以同时为两者设置种子,但是由于如果未设置shadowPath ,它将产生相同的结果,这使我想知道为什么都同时使用它。

采用

popupView.layer.shadowPath = UIBezierPath(roundedRect: popupView.bounds, cornerRadius: 15).cgPath

代替

popupView.layer.shadowPath = UIBezierPath(rect: popupView.bounds).cgPath

并添加一点1.3的shadowOpacity以更好地看到阴影。

您需要将视图图层的masksToBounds值设置为true

popupView.layer.masksToBounds = true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM