【问题标题】:Apply Shadow along with the gradient colour with custom class使用自定义类将阴影与渐变颜色一起应用
【发布时间】:2020-06-09 11:48:11
【问题描述】:

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

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

我想我对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】:

    尝试使用 masksToBounds 而不是 clipsToBounds

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-27
      • 2020-08-11
      • 2021-12-04
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      相关资源
      最近更新 更多