美文网首页
swift view处理圆角阴影

swift view处理圆角阴影

作者: 喵喵粉 | 来源:发表于2020-03-24 10:10 被阅读0次
vBg.extCorner(corners: UIRectCorner(rawValue: UIRectCorner.bottomLeft.rawValue | UIRectCorner.bottomRight.rawValue), radii: kRadii)
//
// MARK: - 处理圆角
//
extension UIView {

    /// 部分圆角
    ///
    /// - Parameters:
    ///   - corners: 需要实现为圆角的角,可传入多个
    ///   - radii: 圆角半径
    func extCorner(corners: UIRectCorner, radii: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radii, height: radii))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        layer.mask = maskLayer
        
//        self.layer.masksToBounds = true
    }
    
    //
    // MARK: - 圆角阴影
    //
    func extShadowColor(_ color: UIColor, radius: CGFloat) {

        //视图没有加上阴影,子视图加上了阴影。后来发现是因为如果不给视图添加背景颜色,
        //阴影就会显示到子视图上,为视图添加背景颜色后,问题解决。
        layer.shadowColor = color.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowOpacity = 0.7//阴影透明度
        layer.shadowRadius = 2//阴影半径
        
        layer.cornerRadius = radius

        //路径阴影
        let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: radius, height: radius))
        //设置阴影路径
        layer.shadowPath = path.cgPath
    }
}

相关文章

网友评论

      本文标题:swift view处理圆角阴影

      本文链接:https://www.haomeiwen.com/subject/wdonyhtx.html