How to control shadow spread and blur?

前端 未结 8 1225
终归单人心
终归单人心 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:33

    For those who are attempting to apply a shadow to a predefined path (Like for a circular view, for instance), here's what I ended up with:

    extension CALayer {
        func applyShadow(color: UIColor = .black,
                         alpha: Float = 0.5,
                         x: CGFloat = 0,
                         y: CGFloat = 2,
                         blur: CGFloat = 4,
                         spread: CGFloat = 0,
                         path: UIBezierPath? = nil) {
            shadowColor = color.cgColor
            shadowOpacity = alpha
            shadowRadius = blur / 2
            if let path = path {
                if spread == 0 {
                    shadowOffset = CGSize(width: x, height: y)
                } else {
                    let scaleX = (path.bounds.width + (spread * 2)) / path.bounds.width
                    let scaleY = (path.bounds.height + (spread * 2)) / path.bounds.height
    
                    path.apply(CGAffineTransform(translationX: x + -spread, y: y + -spread).scaledBy(x: scaleX, y: scaleY))
                    shadowPath = path.cgPath
                }
            } else {
                shadowOffset = CGSize(width: x, height: y)
                if spread == 0 {
                    shadowPath = nil
                } else {
                    let dx = -spread
                    let rect = bounds.insetBy(dx: dx, dy: dx)
                    shadowPath = UIBezierPath(rect: rect).cgPath
                }
            }
            shouldRasterize = true
            rasterizationScale = UIScreen.main.scale
        }
    }
    

    I'll post some examples later, but this has worked spot on for circular views for me.

提交回复
热议问题