UIView带阴影和蒙面图层

时间:2017-12-06 02:41:55

标签: ios swift uiview calayer shadow

所以我有一个UIView(称为 myView )并且应用了一些遮罩。

let maskPath = UIBezierPath(roundedRect: myView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 12, height: 12))
let maskLayer = CAShapeLayer()
maskLayer.frame = myView.bounds
maskLayer.path = maskPath.cgPath
myView.layer.mask = maskLayer

这种布局:

enter image description here

我未能做的是为 myView 添加一些阴影。由于视图的图层有一个蒙版,我无法用阴影向它添加不同的图层。

有没有人遇到过这个问题?

1 个答案:

答案 0 :(得分:2)

将图像颜色设置为清除以及以下内容:

let maskPath = UIBezierPath(roundedRect: redView.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 12, height: 12))
        let maskLayer = CAShapeLayer()[![enter image description here][1]][1]
        maskLayer.fillColor = UIColor.red.cgColor // your color
        maskLayer.frame = redView.bounds
        maskLayer.path = maskPath.cgPath

        redView.layer.addSublayer(maskLayer)

        redView.layer.shadowOffset = CGSize(width: 10, height: 10)
        redView.layer.shadowColor = UIColor.green.cgColor
        redView.layer.shadowOpacity = 1