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

四种设置阴影的方法

    ///实际使用
    func configShadowUI() {
        let shadowV = Bundle.main.loadNibNamed("ShadowCell", owner: self, options: nil)?.first as! ShadowCell
        shadowV.frame.size = CGSize.init(width: screenWidth, height: 108 + 25 / 69 * (screenWidth-30))
        shadowV.center = self.view.center
        shadowV.configShadow()//设置阴影
        self.view.addSubview(shadowV)
    }


//MARK: 设置阴影,圆角,一定要根据 屏幕的尺寸/self的bounds 设定; 不要使用自身尺寸
    ///此方法要在设置尺寸后使用
    func configShadow() {
        configShadow(shadowV: shadowV)
        //configPathShadow(shadowV: shadowV)
        //configPathShadow1(shadowV: shadowV, opacity: 0.8, radius1: 5, radius2: 5)
        //configPathShadow2(shadowV: shadowV)
    }
    
    //MARK: view-->iv、view模式-->方法1
    ///普通设置隐隐
    func configShadow(shadowV: UIView) {
        shadowV.layer.borderWidth = 0.3
        shadowV.layer.borderColor =
            UIColor.groupTableViewBackground.cgColor
        
        //中阴影
        shadowV.layer.shadowColor = UIColor.black.cgColor//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: 给控件设置某个圆角
        //MARK: 设置阴影,圆角,一定要根据 屏幕的尺寸/self的bounds 设定; 不要使用自身尺寸
        let bounds = CGRect.init(x: 0, y: 0, width: self.bounds.width - 30, height: (self.bounds.width - 30) * 29 / 65)
        let maskPath = UIBezierPath.init(roundedRect: 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
        
        /*****设置阴影路径******/
        //MARK: 宽高必须为 相对于屏幕/self的宽高,不能直接使用 控件bounds的宽高
        let pathW: CGFloat = self.bounds.width - 30//shadowV.bounds.width
        let pathH: CGFloat = self.bounds.height - 40//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()
        
        //要设置阴影控件的frame
        //MARK: 必须是 要设置控件 相对于self的frame , 不能直接使用要设置控件的frame
        shadowLayer.frame = CGRect.init(x: 15, y: 20, width: self.bounds.width - 30, height: self.bounds.height - 40)
        
        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
        
        //路径阴影
        //MARK: 宽高必须为 屏幕/self的宽高定,不能使用设置控件的bounds
        let path = UIBezierPath()
        let width = self.bounds.width - 30//shadowV.bounds.width
        let height = self.bounds.height - 40//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) {
        //MARK: 设置阴影,圆角,一定要根据 屏幕的尺寸/self的bounds 设定;不能使用自身尺寸
        let bounds = CGRect.init(x: 0, y: 0, width: self.bounds.width - 30, height: (self.bounds.width - 30) * 29 / 65)
        let maskPath = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 5, height: 5))
        let maskLayer = CAShapeLayer.init()
        maskLayer.path = maskPath.cgPath
        iv.layer.mask = maskLayer
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值