Does anyone have any tips for adding multiple shadows to a view or a view’s 
CGLayer on iOS in either Objectice-C or Swift?

I’ve tried creating multiple CGLayers and adding shadows to each, then 
inserting them in a view, creating multiple views and inserting the view above 
or below the current view.  Nothing works.

What seems to be happening is that when I create another CGLayer or view, there 
is no shadow added to the other CGLayer or view, but the regular view certainly 
gets a shadow.  

FYI, I’m trying to replicate Sketch style shadows in iOS and our designers have 
used multiple external shadows which is why I’m trying for multiple 
views/layers.

Thanks in advance.  
Alex Zavatone

Source:

extension (UIView)  {
    func addShadowToView(
        color: UIColor?,
        alpha: CGFloat,
        x: CGFloat,
        y: CGFloat,
        blur: CGFloat,
        spread: CGFloat
    ) {
        layer.masksToBounds = false
        layer.shadowColor = color?.cgColor
        layer.shadowOpacity = Float(alpha)
        layer.shadowOffset = CGSize(width: x, height: y)
        layer.shadowRadius = blur / UIScreen.main.scale
        if spread == 0 {
            layer.shadowPath = nil
        } else {
            let deltaX = spread
            let shadowRect = bounds.insetBy(dx: deltaX, dy: deltaX)
            layer.shadowPath = CFBridgingRetain(UIBezierPath(rect: shadowRect)) 
as! CGPath
        }
    }
}

extension CALayer {
    func applySketchShadow (
        color: UIColor = .black,
        alpha: Float = 0.5,
        x: CGFloat = 0,
        y: CGFloat = 2,
        blur: CGFloat = 4,
        spread: CGFloat = 0)
    {
        masksToBounds = false
        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
        }
    }
}


And here’s how I’m trying to use it.  Box2 is a UIImageView.  It gets one 
shadow, but never more than one. :/

        alpha = 0.4
        x = 0
        y = 8.0
        blur = 10.0
        spread = 10.0 // 1

        box2.addShadowToView(color: color, alpha: alpha, x: x, y: y, blur: 
blur, spread: spread)

        let myView1 = UIView(frame: box2.bounds)
        myView1.addShadowToView(color: color, alpha: alpha, x: x, y: y, blur: 
blur, spread: spread)
        box2.insertSubview(myView1, belowSubview: box2)

        let myView2 = UIView(frame: box2.bounds)
        myView2.addShadowToView(color: color, alpha: alpha, x: x, y: y, blur: 
blur, spread: spread)
        box2.insertSubview(myView2, belowSubview: box2)


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to