UIView iOS Swift下的多个阴影

问题描述 投票:1回答:3

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

Shadows are above button

我已经做了一个操场来说明这个问题

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()
ios swift uiview calayer shadow
3个回答
1
投票

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

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

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 {
            $0.masksToBounds = false
            $0.frame = layer.bounds
            layer.insertSublayer($0, 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中,因为大多数方法都使用按钮的实际边界。


1
投票

将您的插入更改为:

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

应该这样做。


0
投票

另一种方法是添加双按钮而不更改您的类。

    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{$0.removeFromSuperlayer()}
© www.soinside.com 2019 - 2024. All rights reserved.