添加阴影到按钮和uiview

时间:2018-02-21 06:36:34

标签: ios iphone uiview uibutton shadow

使用以下参数向UIButton和UIView添加阴影。我可以设置不透明度,大小,距离,但它不是完美的。任何人都可以建议如何添加这些。我已尝试过网上提供的其他解决方案,但没有工作。

角度:90, 不透明度13% 距离4px 差价:10% 大小:9px

enter image description here

当我尝试应用以下代码时,这是一种阴影。

self.buttonCategory2.setShadowWithColor(color: UIColor.black, opacity: 0.13, offset: CGSize(width: 9, height: 9), radius: 4, viewCornerRadius: nil)

extension UIView {

/**
 Set a shadow on a UIView.
 - parameters:
 - color: Shadow color, defaults to black
 - opacity: Shadow opacity, defaults to 1.0
 - offset: Shadow offset, defaults to zero
 - radius: Shadow radius, defaults to 0
 - viewCornerRadius: If the UIView has a corner radius this must be set to match
 */
func setShadowWithColor(color: UIColor?, opacity: Float?, offset: CGSize?, radius: CGFloat, viewCornerRadius: CGFloat?) {
    //layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: viewCornerRadius ?? 0.0).CGPath
    layer.shadowColor = color?.cgColor ?? UIColor.black.cgColor
    layer.shadowOpacity = opacity ?? 1.0
    layer.shadowOffset = offset ?? CGSize.zero
    layer.shadowRadius = radius
}
}

enter image description here

必需的暗影

enter image description here

0 个答案:

没有答案