绘制阴影(带圆角可选) (Swift4)

 //MARK: view-->iv、view模式-->方法1

    ///普通设置隐隐

    func configShadow(shadowV: UIView) {

        shadowV.layer.borderWidth = 0.3

        shadowV.layer.borderColor =

            UIColor.groupTableViewBackground.cgColor

        

        //中阴影

        shadowV.layer.shadowColor = UIColor.init(hexColor: "A5A5A5").cgColor

        shadowV.layer.shadowOpacity = 0.5//不透明度

        shadowV.layer.shadowRadius = 5.0//设置阴影所照射的范围

        shadowV.layer.shadowOffset = CGSize.init(width: 0, height: 0)// 设置阴影的偏移量

        

        //后设置圆角

        shadowV.layer.cornerRadius = 5

        

        //iv.layer.masksToBounds = true

        //MARK: 给控件设置某个圆角

        let maskPath = UIBezierPath.init(roundedRect: iv.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 5, height: 5))

        let maskLayer = CAShapeLayer.init()

        maskLayer.path = maskPath.cgPath

        iv.layer.mask = maskLayer

     

    }

    

    //MARK: 设定路径阴影--> 方法2

    ///设置路径阴影--->只通过直线--->设置固定值

    func configPathShadow(shadowV: UIView) {

        shadowV.layer.shadowColor = UIColor.gray.cgColor//阴影颜色

        shadowV.layer.shadowOpacity = 0.8//阴影透明度

        shadowV.layer.shadowRadius = 5.0//阴影圆角

        shadowV.layer.shadowOffset = CGSize.init(width: 0, height: 0)//偏移量

        

        //设置圆角

        shadowV.layer.cornerRadius = 5.0

        

        /*****设置阴影路径******/

        let pathW: CGFloat = shadowV.bounds.width

        let pathH: CGFloat = shadowV.bounds.height

        

        let path = UIBezierPath()

        path.move(to: CGPoint.init(x: -5, y: -5))


        //添加直线

        path.addLine(to: CGPoint.init(x: pathW / 2, y: -7))

        path.addLine(to: CGPoint.init(x: pathW + 5, y: -5))

       

        path.addLine(to: CGPoint.init(x: pathW + 7, y: pathH / 2))

        path.addLine(to: CGPoint.init(x: pathW + 5, y: pathH + 5))

        path.addLine(to: CGPoint.init(x: pathW / 2, y: pathH + 7))

        

        path.addLine(to: CGPoint.init(x: -5, y: pathH + 5))

        path.addLine(to: CGPoint.init(x: -7, y: pathH / 2))

        path.addLine(to: CGPoint.init(x: -5, y: -5))

        

        //设置阴影路径

        shadowV.layer.shadowPath = path.cgPath

        

        /***给控件设置某个圆角***/

        configSideRadius(iv: iv)

    }

    

    //MARK: 设置路径阴影--> 方法3

    ///设置阴影路径--->通过圆角和直线

    func configPathShadow1(shadowV: UIView, opacity: Float = 0.8, radius1: CGFloat = 8, radius2: CGFloat = 8) {

        let shadowOpacity: Float = opacity

        let shadowRadius: CGFloat = radius1

        let cornerRadius: CGFloat = radius2

        

        let shadowLayer = CALayer.init()

        shadowLayer.frame = shadowV.frame

        

        shadowLayer.shadowColor = UIColor.black.cgColor

        shadowLayer.shadowOffset = CGSize.init(width: 0, height: 0)//阴影偏移量,默认(0, -3),跟shadowRadius配合使用

        shadowLayer.shadowOpacity = shadowOpacity//阴影透明度,默认0

        shadowLayer.shadowRadius = shadowRadius//阴影半径,默认3

        

        //路劲阴影

        let path = UIBezierPath()

        

        let width = shadowLayer.bounds.size.width

        let height = shadowLayer.bounds.size.height

        let x = shadowLayer.bounds.origin.x

        let y = shadowLayer.bounds.origin.y

        

        let topleft = shadowLayer.bounds.origin

        let topRight = CGPoint.init(x: x + width, y: y)

        let bottomRight = CGPoint.init(x: x + width, y: y + height)

        let bottomLeft = CGPoint.init(x: x, y: y + height)

        

        let offset: CGFloat = -1

        path.move(to: CGPoint.init(x: topleft.x - offset, y: topleft.y + cornerRadius))

        

        path.addArc(withCenter: CGPoint.init(x: topleft.x + cornerRadius, y: topleft.y + cornerRadius), radius: cornerRadius + offset, startAngle: CGFloat.pi, endAngle: CGFloat.pi / 2 * 3, clockwise: true)

        path.addLine(to: CGPoint.init(x: topRight.x - cornerRadius, y: topRight.y - offset))

        

        path.addArc(withCenter: CGPoint.init(x: topRight.x - cornerRadius, y: topRight.y + cornerRadius), radius: cornerRadius + offset, startAngle: CGFloat.pi / 2 * 3, endAngle: CGFloat.pi * 2, clockwise: true)

        path.addLine(to: CGPoint.init(x: bottomRight.x + offset, y: bottomRight.y - cornerRadius))

        

        path.addArc(withCenter: CGPoint.init(x: bottomRight.x - cornerRadius, y: bottomRight.y - cornerRadius), radius: cornerRadius + offset, startAngle: 0, endAngle: CGFloat.pi / 2, clockwise: true)

        path.addLine(to: CGPoint.init(x: bottomLeft.x + cornerRadius, y: bottomLeft.y + offset))

        

        path.addArc(withCenter: CGPoint.init(x: bottomLeft.x + cornerRadius, y: bottomLeft.y - cornerRadius), radius: cornerRadius + offset, startAngle: CGFloat.pi / 2, endAngle: CGFloat.pi, clockwise: true)

        path.addLine(to: CGPoint.init(x: topleft.x - offset, y: topleft.y + cornerRadius))

        

        //设置阴影路径

        shadowLayer.shadowPath = path.cgPath

        

        shadowV.layer.cornerRadius = cornerRadius

        shadowV.layer.masksToBounds = true

        shadowV.layer.rasterizationScale = UIScreen.main.scale

        

        shadowV.superview?.layer.insertSublayer(shadowLayer, below: shadowV.layer)

    }

    

    //MARK: 设置路径阴影--->方法4

    ///设置普通路径阴影

    func configPathShadow2(shadowV: UIView) {

        shadowV.layer.shadowColor = UIColor.gray.cgColor

        shadowV.layer.shadowOffset = CGSize.init(width: 0, height: 0)

        shadowV.layer.shadowOpacity = 0.8//0.8,默认0,阴影透明度

        shadowV.layer.shadowRadius = 5//8,阴影半径,默认3

        

        //切圆角

        shadowV.layer.cornerRadius = 5

        

        //路径阴影

        let path = UIBezierPath()

        let width = shadowV.bounds.width

        let height = shadowV.bounds.size.height

        let x = shadowV.bounds.origin.x

        let y = shadowV.bounds.origin.y

        

        let topLeft = shadowV.bounds.origin

        let topRight = CGPoint.init(x: x + width, y: y)

        let bottomRight = CGPoint.init(x: x + width, y: y + height)

        let bottomLeft = CGPoint.init(x: x, y: y + height)

        

        let offset: CGFloat = 0.0

        path.move(to: CGPoint.init(x: topLeft.x - offset, y: topLeft.y - offset))

        path.addLine(to: CGPoint.init(x: topRight.x + offset, y: topRight.y - offset))

        path.addLine(to: CGPoint.init(x: bottomRight.x + offset, y: bottomRight.y + offset))

        path.addLine(to: CGPoint.init(x: bottomLeft.x - offset, y: bottomLeft.y + offset))

        path.addLine(to: CGPoint.init(x: topLeft.x - offset, y: topLeft.y - offset))

        

        //设置阴影路径

        shadowV.layer.shadowPath = path.cgPath

        

        //MARK: 给控件设置某个圆角

        configSideRadius(iv: iv)

    }

    

    //MARK: 设置某个圆角

    func configSideRadius(iv: UIView) {

        let maskPath = UIBezierPath.init(roundedRect: iv.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 5, height: 5))

        let maskLayer = CAShapeLayer.init()

        maskLayer.path = maskPath.cgPath

        iv.layer.mask = maskLayer

    }

    

猜你喜欢

转载自blog.csdn.net/flyingfirefish/article/details/80694930