How to control shadow spread and blur?

前端 未结 8 1146
终归单人心
终归单人心 2020-12-07 06:51

I have designed UI elements in sketch, and one of them has a shadow with blur 1 and spread 0. I looked at the doc for the views layer property and layer doesnt have anything

相关标签:
8条回答
  • 2020-12-07 07:13

    You can try this .... you can play with the values. The shadowRadius dictates the amount of blur. shadowOffset dictates where the shadow goes.

    Swift 2.0

    let radius: CGFloat = demoView.frame.width / 2.0 //change it to .height if you need spread for height
    let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 2.1 * radius, height: demoView.frame.height))
    //Change 2.1 to amount of spread you need and for height replace the code for height
    
    demoView.layer.cornerRadius = 2
    demoView.layer.shadowColor = UIColor.blackColor().CGColor
    demoView.layer.shadowOffset = CGSize(width: 0.5, height: 0.4)  //Here you control x and y
    demoView.layer.shadowOpacity = 0.5
    demoView.layer.shadowRadius = 5.0 //Here your control your blur
    demoView.layer.masksToBounds =  false
    demoView.layer.shadowPath = shadowPath.CGPath
    

    Swift 3.0

    let radius: CGFloat = demoView.frame.width / 2.0 //change it to .height if you need spread for height 
    let shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 2.1 * radius, height: demoView.frame.height)) 
    //Change 2.1 to amount of spread you need and for height replace the code for height
    
    demoView.layer.cornerRadius = 2
    demoView.layer.shadowColor = UIColor.black.cgColor
    demoView.layer.shadowOffset = CGSize(width: 0.5, height: 0.4)  //Here you control x and y
    demoView.layer.shadowOpacity = 0.5
    demoView.layer.shadowRadius = 5.0 //Here your control your blur
    demoView.layer.masksToBounds =  false
    demoView.layer.shadowPath = shadowPath.cgPath
    

    Example with spread

    To create a basic shadow

        demoView.layer.cornerRadius = 2
        demoView.layer.shadowColor = UIColor.blackColor().CGColor
        demoView.layer.shadowOffset = CGSizeMake(0.5, 4.0); //Here your control your spread
        demoView.layer.shadowOpacity = 0.5 
        demoView.layer.shadowRadius = 5.0 //Here your control your blur
    

    Basic Shadow example in Swift 2.0

    0 讨论(0)
  • 2020-12-07 07:18

    Sketch Shadow Using IBDesignable and IBInspectable in Swift 4

    SKETCH AND XCODE SIDE BY SIDE

    CODE

    @IBDesignable class ShadowView: UIView {
    
        @IBInspectable var shadowColor: UIColor? {
            get {
                if let color = layer.shadowColor {
                    return UIColor(cgColor: color)
                }
                return nil
            }
            set {
                if let color = newValue {
                    layer.shadowColor = color.cgColor
                } else {
                    layer.shadowColor = nil
                }
            }
        }
    
        @IBInspectable var shadowOpacity: Float {
            get {
                return layer.shadowOpacity
            }
            set {
                layer.shadowOpacity = newValue
            }
        }
    
        @IBInspectable var shadowOffset: CGPoint {
            get {
                return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)
            }
            set {
                layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
            }
    
         }
    
        @IBInspectable var shadowBlur: CGFloat {
            get {
                return layer.shadowRadius
            }
            set {
                layer.shadowRadius = newValue / 2.0
            }
        }
    
        @IBInspectable var shadowSpread: CGFloat = 0 {
            didSet {
                if shadowSpread == 0 {
                    layer.shadowPath = nil
                } else {
                    let dx = -shadowSpread
                    let rect = bounds.insetBy(dx: dx, dy: dx)
                    layer.shadowPath = UIBezierPath(rect: rect).cgPath
                }
            }
        }
    }
    

    OUTPUT

    HOW TO USE IT

    0 讨论(0)
  • 2020-12-07 07:18

    It might be a little diggin in history, but maybe some had same issue. I useed code sample from accepted answer. However the effects are quite different: - y value has to be around half compared to same value in sketch - I tried to apply shadow on navigation bar and the effect is terribly different - barely visible while using same values that sketch had.

    So there seems that the method is totally not reflecting sketch parameters. Any hints?

    0 讨论(0)
  • 2020-12-07 07:25

    I really like the answer posted here and suggestions in the comments. This is how I modified that solution:

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

    USAGE:

    myButton.applyShadow(color: .black, alpha: 0.2, x: 0, y: 1, blur: 2, spread: 0)
    
    0 讨论(0)
  • 2020-12-07 07:26

    My solution based on this post replies: (Swift 3)

    let shadowPath = UIBezierPath(rect: CGRect(x: -1,
                                               y: -2,
                                               width: target.frame.width + 2,
                                               height: target.frame.height + 2))
    
    target.layer.shadowColor = UIColor(hexString: shadowColor).cgColor
    target.layer.shadowOffset = CGSize(width: CGFloat(shadowOffsetX), height: CGFloat(shadowOffsetY))
    target.layer.masksToBounds = false
    target.layer.shadowOpacity = Float(shadowOpacity)
    target.layer.shadowPath = shadowPath.cgPath
    
    0 讨论(0)
  • 2020-12-07 07:29

    This code worked very well for me:

    yourView.layer.shadowOpacity = 0.2 // opacity, 20%
    yourView.layer.shadowColor = UIColor.black.cgColor
    yourView.layer.shadowRadius = 2 // HALF of blur
    yourView.layer.shadowOffset = CGSize(width: 0, height: 2) // Spread x, y
    yourView.layer.masksToBounds = false
    
    0 讨论(0)
提交回复
热议问题