将阴影与渐变色一起使用自定义类

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

我为渐变按钮编写了一个自定义类,我也想为此应用阴影。

我得到渐变色,但没有阴影。

[我认为我对self.clipsToBounds = true有问题

通过以下内容找到我的代码。

class GradientButton: UIButton {

    let gradientLayer = CAGradientLayer()

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    func commonInit() -> Void {

        layer.cornerRadius = self.frame.size.height / 2.0
        gradientLayer.locations = [0.0, 1.0]
        // top-right to bottom-left
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)

        // we only add the layer once, here when called from init
        layer.addSublayer(gradientLayer)
        self.clipsToBounds = true
    }

    func setGradientBackground(colorOne: UIColor, colorTwo: UIColor) {
        gradientLayer.colors = [colorOne.cgColor, colorTwo.cgColor]
        applyShadow(shadoeclr: colorOne)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = bounds
    }

    func applyShadow(shadoeclr:UIColor){
        layer.shadowColor = shadoeclr.cgColor
        layer.shadowRadius = 5
        layer.shadowOpacity = 0.5
        layer.shadowOffset = CGSize(width: 0, height: 0)
    }
}
ios swift colors gradient
1个回答
0
投票

尝试使用masksToBounds代替clipsToBounds

© www.soinside.com 2019 - 2024. All rights reserved.