iOS Swift如何为透明的UIView添加阴影

时间:2017-10-22 03:51:59

标签: ios swift uiview

我使用UIView作为带阴影的叠加层,但是一旦我将UIView背景颜色设置为.clear,我就不再看到阴影,是否有办法为具有清晰背景颜色的UIView添加阴影?< / p>

1 个答案:

答案 0 :(得分:-2)

是通过给出不透明度层小于10

来实现

@IBInspectable var shadowOffsetX:CGFloat = 0

@IBInspectable var shadowOffsetY : CGFloat = 0

@IBInspectable var shadowColor : UIColor? {
    didSet{
        self.layer.shadowColor = shadowColor?.cgColor
    }
}

@IBInspectable var shadowOpacity : Float = 0 {
    didSet{
        self.layer.shadowOpacity = shadowOpacity
    }
}

@IBInspectable var shadowRadius : CGFloat = 0 {
    didSet{
        self.layer.shadowRadius = shadowRadius
    }
}

@IBInspectable var cornerRadius : CGFloat = 0 {
    didSet{
        layer.cornerRadius = cornerRadius
        layer.masksToBounds = true
    }
}

@IBInspectable var borderWidth : CGFloat = 0 {
    didSet{
        layer.borderWidth = borderWidth
    }
}

@IBInspectable var borderColor : UIColor? {
    didSet{
        layer.borderColor = borderColor?.cgColor
    }
}

@IBInspectable var layerOpacity : Float = 0 {
    didSet{
        self.layer.opacity = layerOpacity
    }
}

override func awakeFromNib() {
    super.awakeFromNib()
    self.layer.shadowOffset = CGSize(width: shadowOffsetX, height: shadowOffsetY)
}