繁体   English   中英

如何设置角半径,边框和阴影的UIView蒙版?

[英]How to set UIView mask with corner radius, border and shadow?

我想要实现的是将shadowbordercornerRadiusUIView一起应用。

到现在cornerRadius ,我可以设置maskcornerRadiusborder 问题是尝试设置shadow

到目前为止这是我的代码

let path = UIBezierPath(roundedRect: rect, byRoundingCorners: UIRectCorner.allCorners, cornerRadii: CGSize(width: radius, height: radius))

let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask

let frameLayer = CAShapeLayer()
frameLayer.path = path.cgPath
frameLayer.lineWidth = width
frameLayer.strokeColor = color.cgColor
frameLayer.fillColor = nil

layer.addSublayer(frameLayer)

问题是如果我尝试将shadow设置为frameLayer或layer,我没有得到理想的结果。 它会被蒙面修剪掉。 有没有办法实现这个,除了使用下一行代码?

layer.cornerRadius = 5
layer.borderColor = color.cgColor
layer.borderWidth = width
layer.shadowColor = shadowColor.cgColor
layer.shadowOpacity = opacity
layer.shadowRadius = radius
layer.shadowOffset = offset
clipsToBounds = true

两个片段都打算在UIView子类中使用,所以如果有人可以帮我解决这个问题,我会非常感激。 谢谢

如果还有其他需要或不清楚的地方请告诉我

嘿,半小时后我设法找到解决我自己问题的方法:

let path = UIBezierPath(roundedRect: rect, byRoundingCorners: UIRectCorner.allCorners, cornerRadii: CGSize(width: radius, height: radius))

let frameLayer = CAShapeLayer()
frameLayer.path = path.cgPath
frameLayer.shadowPath = path.cgPath

frameLayer.lineWidth = borderWidth
frameLayer.strokeColor = borderColor.cgColor
frameLayer.fillColor = backgroundColor?.cgColor
frameLayer.shadowOffset = shadowOffset
frameLayer.shadowOpacity = shadowOpacity
frameLayer.shadowRadius = shadowRadius
frameLayer.shadowColor = shadowColor.cgColor

layer.mask = frameLayer
layer.insertSublayer(frameLayer, at: 0)

暂无
暂无

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

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