如何在 swift 中为 UIView 添加选定的圆角半径和阴影

How to add selected corner radius and shadow for UIView in swift

有人可以在 iOS Swift 中帮助我解决这个问题吗?我有一个带有投影效果的 UI 视图。除了一个角外,我需要 3 个角的角半径。感谢任何帮助。

谢谢, 陀罗尼

您可以在 UIView 上使用扩展:

这是我在 Playgrounds 上创建的示例:

let view = UIView()

extension UIView {
    func roundCorners(_ corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}
// customize where you want your roundCorners on your UIView
view.roundCorners([.topLeft, .topRight, .bottomRight], radius: 10)