首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAGradientLayer不显示

CAGradientLayer不显示
EN

Stack Overflow用户
提问于 2018-11-20 02:05:14
回答 3查看 2.7K关注 0票数 5

我已经看过很多这样的问题的答案,但是没有一个答案能解决我的问题,所以我需要一点帮助。

我试图将垂直梯度应用于UIButton,但是渐变层在我的视图中没有显示出来。以下是相关代码:

代码语言:javascript
复制
let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)

let addButton = UIButton(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
addButton.title = "Start counting"
addButton.layer.cornerRadius = 30.0
addButton.layer.borderWidth = 1.0
addButton.layer.borderColor = darkPurple.cgColor
addButton.translatesAutoresizingMaskIntoConstraints = false
myView.addSubview(addButton)
addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)

...and用于应用梯度的代码:

代码语言:javascript
复制
typealias GradientPoints = (startPoint: CGPoint, endPoint: CGPoint)

enum GradientOrientation {
  case topRightBottomLeft
  case topLeftBottomRight
  case horizontal
  case vertical

  var startPoint: CGPoint {
    return points.startPoint
  }

  var endPoint: CGPoint {
    return points.endPoint
  }

  var points: GradientPoints {
    switch self {
    case .topRightBottomLeft:
      return (CGPoint(x: 0.0, y: 1.0), CGPoint(x: 1.0, y: 0.0))
    case .topLeftBottomRight:
      return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 1.0))
    case .horizontal:
      return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 1.0, y: 0.0))
    case .vertical:
      return (CGPoint(x: 0.0, y: 0.0), CGPoint(x: 0.0, y: 1.0))
    }
  }
}

extension UIView {
  func applyGradient(with colors: [UIColor], gradientOrientation orientation: GradientOrientation) {
    let gradient = CAGradientLayer()
    gradient.frame = self.bounds
    gradient.colors = colors.map { $0.cgColor }
    gradient.startPoint = orientation.startPoint
    gradient.endPoint = orientation.endPoint
    gradient.borderColor = self.layer.borderColor
    gradient.borderWidth = self.layer.borderWidth
    gradient.cornerRadius = self.layer.cornerRadius
    gradient.masksToBounds = true
    gradient.isHidden = false

    self.layer.insertSublayer(gradient, at: 0)
  }
}

这就是我得到的:空按钮

谢谢你的帮忙!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-11-20 02:27:09

自动布局不影响UIView对象的层,因此在计算UIButton帧之前,梯度层被过早地应用。当指定梯度层的frame属性时,按钮的边界仍然等于CGRect.zero,并且梯度层帧以后永远不会更新。

您会发现,如果在计算了按钮框架后添加了梯度层,它就会像预期的那样工作。例如:

代码语言:javascript
复制
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let darkPurple = UIColor(displayP3Red: 61.0/255.0, green: 3.0/255.0, blue: 110.0/255.0, alpha: 1.0)
    let lightPurple = UIColor(displayP3Red: 90.0/255.0, green: 32.0/255.0, blue: 130.0/255.0, alpha: 1.0)

    addButton.applyGradient(with: [lightPurple, darkPurple], gradientOrientation: .vertical)
}

另一个选项是更新viewDidLayoutSubviews()中渐变层的viewDidLayoutSubviews()属性或创建自定义UIButton子类,并在按钮帧更新时更新渐变层帧。

参考文献

票数 3
EN

Stack Overflow用户

发布于 2021-08-09 07:54:01

对于那些已经花了好几个小时搜索的人(就像我一样):

请确保您的颜色数组包含CGColor而不是UIColor,如文档所示:

颜色 定义每个渐变停止的颜色的CGColorRef对象数组。动画片。

https://developer.apple.com/documentation/quartzcore/cagradientlayer/1462403-colors

奖励:如果你再次错过这个小代码片段,你会崩溃的(推荐使用#if DEBUG )。

代码语言:javascript
复制
extension CAGradientLayer {
    dynamic var colors: [Any]? {
        get { self.value(forKey: "colors") as? [Any] }
        set {
            if let newValue = newValue {
                for color in newValue {
                    if color is UIColor {
                        fatalError("CAGradientLayer.colors must contain only CGColor! UIColor was found")
                    }
                }
            }
            super.setValue(newValue, forKey: "colors")
        }
    }
}
票数 12
EN

Stack Overflow用户

发布于 2018-11-20 02:17:39

亚层的排列顺序是从后到前。在索引0处插入一个层会将其放置在任何其他子层之后。我从来没有尝试过用按钮来做这件事,但怀疑它至少有一个子层,覆盖了你想要添加的渐变。

尝试使用addSublayer(),这将把该层放在子层数组的顶部。

编辑:

我构建了您的代码的一个变体,当我将self.layer.insertSublayer(_:at:)更改为addSublayer()时,它可以工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53385203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档