0

I'm trying to add a custom shadow to my uiview with a gradient in a collection cell, but I seem to be doing things backwards... I added this class EHGradientView to my mainview in UICollectionView. And by changing the class that seemed to work on my individual items. But the shadow is proving difficult.

IBDesignable
class EHGradientView: UIView {


        override class var layerClass: AnyClass {
            return CAGradientLayer.self
        }

        private var gLayer: CAGradientLayer {
            return self.layer as! CAGradientLayer
        }

        override init(frame: CGRect) {
            super.init(frame: frame)
            commonInit()
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            commonInit()
        }

        func commonInit() {

            gLayer.type = .axial

            gLayer.colors = [
                UIColor(red: 1, green: 0.46, blue: 0.467, alpha: 1).cgColor,

                UIColor(red: 1, green: 0.36, blue: 0.369, alpha: 1).cgColor

            ]
            gLayer.locations = [0, 1]
            gLayer.startPoint = CGPoint(x: 0.25, y: 0.5)
            gLayer.endPoint = CGPoint(x: 0.75, y: 0.5)

            // exaggerated colors so it's obvious

            gLayer.cornerRadius = 28

            shadow()


        }

      func shadow() {
         var shadows = UIView()

        shadows.frame = frame

        shadows.clipsToBounds = false

        addSubview(shadows)


        let shadowPath0 = UIBezierPath(roundedRect: shadows.bounds, cornerRadius: 10)

        let layer0 = CALayer()

        layer0.shadowPath = shadowPath0.cgPath

        layer0.shadowColor = UIColor(red: 1, green: 0.36, blue: 0.369, alpha: 0.4).cgColor

        layer0.shadowOpacity = 1

        layer0.shadowRadius = 20

        layer0.shadowOffset = CGSize(width: 0, height: 5)

        layer0.bounds = shadows.bounds

        layer0.position = shadows.center

        shadows.layer.addSublayer(layer0)


        var shapes = UIView()

        shapes.frame = frame

        shapes.clipsToBounds = true

        addSubview(shapes)


        let layer1 = CALayer()

        layer1.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 1).cgColor

        layer1.bounds = shapes.bounds

        layer1.position = shapes.center

        shapes.layer.addSublayer(layer1)


        shapes.layer.cornerRadius = 10



    }


This is the shadow code I keep trying to combine to it so that the shadow appears UNDERNEATH it and ends up looking like this. enter image description here

Instead what I'm getting is this:

enter image description here

KFDoom
  • 772
  • 1
  • 6
  • 19

1 Answers1

0

Below code may help you.

backgroundColor = .systemPink
layer.cornerRadius = 20
layer.masksToBounds = false
layer.shadowColor = UIColor.red.cgColor
layer.shadowOpacity = 0.3
layer.shadowRadius = 9.0
layer.shadowOffset = CGSize(width: 1, height: 5)

modified properties values and got this result. May this help you.