ios – 在UIView上添加阴影

前端之家收集整理的这篇文章主要介绍了ios – 在UIView上添加阴影前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在我的UIView顶部添加阴影我尝试过以下但没有运气……
childView.layer.cornerRadius = 5;
childView.layer.masksToBounds = YES;
childView.layer.shadowOffset = CGSizeMake(-15,20);
childView.layer.shadowRadius = 5;
childView.layer.shadowOpacity = 0.5;

解决方法

Swift 3扩展:

包括我正在处理的应用的默认值,但您可以更改它们以匹配您在应用中所需的样式.

enum VerticalLocation: String {
    case bottom
    case top  
}

extension UIView {
    func addShadow(location: VerticalLocation,color: UIColor = .black,opacity: Float = 0.5,radius: CGFloat = 5.0) {
        switch location {
        case .bottom:
             addShadow(offset: CGSize(width: 0,height: 10),color: color,opacity: opacity,radius: radius)
        case .top:
            addShadow(offset: CGSize(width: 0,height: -10),radius: radius)
        }
    }

    func addShadow(offset: CGSize,radius: CGFloat = 5.0) {
        self.layer.masksToBounds = false
        self.layer.shadowColor = color.cgColor
        self.layer.shadowOffset = offset
        self.layer.shadowOpacity = opacity
        self.layer.shadowRadius = radius
    }
}
原文链接:https://www.f2er.com/iOS/330883.html

猜你在找的iOS相关文章