OGeek|极客世界-中国程序员成长平台

标题: ios - 如何向 ASDisplayNode 或 ASButtonNode 添加渐变 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:08
标题: ios - 如何向 ASDisplayNode 或 ASButtonNode 添加渐变

我希望将渐变背景添加到 ASDisplayNode/ASButtonNode。 我已经尝试创建一个渐变层并将其添加为像这样的子层 -

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = button.frame;
gradient.colors = @[[UIColor redColor], [UIColor blueColor]];
[button.view.layer insertSublayer:gradient atIndex:0];

其中 button 的类型是 ASButtonNode,但这只是为按钮提供了白色背景。我也找不到太多实现这一目标的文档。

如何在给定 UIColor 数组和 CGFloat 角度的情况下添加背景?

谢谢



Best Answer-推荐答案


swift 3.

extension UIView {

       func gradient(color1: UIColor, color2: UIColor) -> CAGradientLayer {
             let gradient: CAGradientLayer = CAGradientLayer()
             gradient.colors = [color1.cgColor, color2.cgColor]
             gradient.locations = [0.0 , 1.0]
             gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
             gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
             gradient.frame = CGRect(x: 0.0, y: 0.0, width: frame.size.width, height: frame.size.height)
             return gradient
          } 
    }

例子:

let gradient = self.cardGradientNode.view.gradient(
    color1: gradient.start, 
    color2: gradient.end
)
self.cardGradientNode.view.layer.insertSublayer(gradient, at: 0)

结果: enter image description here

关于ios - 如何向 ASDisplayNode 或 ASButtonNode 添加渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43321357/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4