I want to "cut a hole" in the shadow layer of a UIView an Swift3, iOS
I have a container (UIView), that has 2 children:
- one UIImageView
- one UIView on top of that image ("overlay")
I want to give the overlay a shadow and cut out an inner rect of that shadow, to create a glow-like effect at the edges of the ImageView
It is crucial that the glow is inset, since the image is taking the screen width
My code so far:
let glowView = UIView(frame: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight))
glowView.layer.shadowPath = UIBezierPath(roundedRect: container.bounds, cornerRadius: 4.0).cgPath
glowView.layer.shouldRasterize = true
glowView.layer.rasterizationScale = UIScreen.main.scale
glowView.layer.shadowOffset = CGSize(width: 1.0, height: 1.0)
glowView.layer.shadowOpacity = 0.4
container.addSubview(imageView)
container.addSubview(glowView)
The result looks like the following right now:
Now I would like to cut out the darker inner part, so that just the shadow at the edges remains
Any idea how to achieve this?
Try using this as as your shadow path:
This won't create a rounded rect but with a little bit of changes to the code above you should be able to add those too.
Thanks to Mihai Fratu's answer I was able to create a UIBezierPath that exactly meets my needs
I post my code here in case somebody has the same problem later
These days it's very easy to do this.
Here's the whole thing