UIView 下的多个阴影 iOS Swift

Multiple shadows under UIView iOS Swift

我正在尝试制作一个带有 2 个彩色阴影的圆角 UIButton。为什么是红色的(此时还有覆盖按钮的蓝色“阴影”层?如何获得按钮下方的阴影canvas)。我认为它有助于插入子层而不是仅仅添加它们。

我做了一个 playground 来说明这个问题

import UIKit
import PlaygroundSupport

这是我要实现的按钮

class PrimaryButton: UIButton {
    required init(text: String = "Test 1", hasShadow: Bool = true) {
        super.init(frame: .zero)
        setTitle(text, for: .normal)
        backgroundColor = UIColor.blue
        layer.cornerRadius = 48 / 2
        layer.masksToBounds = false
        if hasShadow {
            insertShadow()
        }
    }
    
    fileprivate func insertShadow() {
        let layer2 = CALayer(layer: layer), layer3 = CALayer(layer: layer)
        layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
        layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)
        layer.insertSublayer(layer2, at: 0)
        layer.insertSublayer(layer3, at: 0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layer.sublayers?.forEach { (sublayer) in
            sublayer.shadowPath = UIBezierPath(rect: bounds).cgPath
        }
    }
}

这是一个有助于添加 Sketch 规范阴影的扩展:

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


class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        
        let button = PrimaryButton()
        button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
        view.addSubview(button)
        self.view = view
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

这对我来说似乎是合法的。 layer1 & layer2 是按钮层的子层。

您可以添加第三层作为背景。这是一个基于您的代码的示例:

class PrimaryButton: UIButton {

    let layer1 = CALayer(), layer2 = CALayer(), layer3 = CALayer()

    override func layoutSubviews() {
        super.layoutSubviews()
        layer1.backgroundColor = UIColor.blue.cgColor
        layer1.cornerRadius = 48 / 2
        [layer1, layer2, layer3].forEach {
            [=10=].masksToBounds = false
            [=10=].frame = layer.bounds
            layer.insertSublayer([=10=], at: 0)
        }
        layer2.applySketchShadow(color: UIColor.red, alpha: 0.5, x: 0, y: 15, blur: 35, spread: -10)
        layer3.applySketchShadow(color: UIColor.blue, alpha: 0.5, x: 0, y: 10, blur: 21, spread: -9)
    }
}

请注意,我将大部分代码放在 layoutSubviews 中,因为您的大多数方法都使用按钮的实际边界。

将您的插入更改为:

    layer.insertSublayer(layer2, at: 1)
    layer.insertSublayer(layer3, at: 2)

应该这样做。

另一种方法是在不更改 class 的情况下添加双按钮。

    let button = PrimaryButton()
    button.frame = CGRect(x: 150, y: 200, width: 200, height: 48)
    button.backgroundColor = UIColor.clear
    view.addSubview(button)
    self.view = view
    let button1 = PrimaryButton()
    button1.frame = CGRect(x: 0, y: 0, width: 200, height: 48)
    button.addSubview(button1)
    button1.layer.sublayers?.forEach{[=10=].removeFromSuperlayer()}