将阴影和圆角添加到UIView

时间:2018-05-03 14:30:29

标签: ios swift uiview calayer

我有以下扩展名,允许我围绕视图的特定角落:

extension CALayer {

    func roundCorners(corners: UIRectCorner, radius: CGFloat, viewBounds: CGRect) {

        let maskPath = UIBezierPath(roundedRect: viewBounds,
                                    byRoundingCorners: corners,
                                    cornerRadii: CGSize(width: radius, height: radius))

        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        mask = shape
    }
}

然后使用它:

innerView.layer.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 20, viewBounds: innerView.bounds)

完美无缺。

但是,如果我还想在该视图中添加阴影,则不会绘制阴影,例如:

override func viewDidAppear(_ animated: Bool) {
...
        innerView.layer.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 20, viewBounds: innerView.bounds)
innerView.layer.shadowColor = UIColor.gray.cgColor
        innerView.layer.shadowOpacity = 0.7
        innerView.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
        innerView.layer.shadowRadius = 2
...
}

视图中的结果只是圆角但没有阴影。如何创建所需的阴影并能够围绕特定角落(而不是一次全部4个角落)

0 个答案:

没有答案