我希望实现的目标是为
以下是我的代码:
问题在于,如果我尝试将
UIView
应用遮罩,同时添加shadow
、border
和cornerRadius
。到目前为止,我已经能够设置mask
、cornerRadius
和border
,但问题在于尝试设置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
子类中使用的,如果有人能够帮助我解决这个问题,我将不胜感激。谢谢。
如果需要其他信息或有不清楚的地方,请告诉我。
UIView
的完整代码?@Baki - Reinier Melian