Swift - Problems with corner radius and drop shadow

前端 未结 14 1453
失恋的感觉
失恋的感觉 2020-11-28 18:35

I\'m trying to create a button with rounded corners and a drop shadow. No matter how I switch up, the button will not display correctly. I\

14条回答
  •  猫巷女王i
    2020-11-28 18:44

    Corner Radius with Shadow

    Short and simple way !!!!!

    extension CALayer {
        func applyCornerRadiusShadow(
            color: UIColor = .black,
            alpha: Float = 0.5,
            x: CGFloat = 0,
            y: CGFloat = 2,
            blur: CGFloat = 4,
            spread: CGFloat = 0,
            cornerRadiusValue: CGFloat = 0)
        {
            cornerRadius = cornerRadiusValue
            shadowColor = color.cgColor
            shadowOpacity = alpha
            shadowOffset = CGSize(width: x, height: y)
            shadowRadius = blur / 2.0
            if spread == 0 {
                shadowPath = nil
            } else {
                let dx = -spread
                let rect = bounds.insetBy(dx: dx, dy: dx)
                shadowPath = UIBezierPath(rect: rect).cgPath
            }
        }
    

    Use of code

    btn.layer.applyCornerRadiusShadow(color: .black, 
                                alpha: 0.38, 
                                x: 0, y: 3, 
                                blur: 10, 
                                spread: 0, 
                                cornerRadiusValue: 24)
    

    No need maskToBound

    Please verify clipsToBounds is false.

    OUTPUT

提交回复
热议问题