渐变色 Swift

Gradient Color Swift

这是我正在尝试做的事情:

截图来自Iphone:

这是我的代码:

@IBOutlet weak var viewBottomBorder: UIView!

let gradient = CAGradientLayer()
gradient.startPoint = CGPoint(x: 0.5, y: 0.0)
gradient.endPoint = CGPoint(x: 0.5, y: 1.0)
let whiteColor = UIColor.black
gradient.colors = [whiteColor.withAlphaComponent(0.0).cgColor, whiteColor.withAlphaComponent(1.0), whiteColor.withAlphaComponent(1.0).cgColor]
gradient.locations = [NSNumber(value: 0.0),NSNumber(value: 0.2),NSNumber(value: 1.0)]
gradient.frame = viewBottomBorder.bounds
viewBottomBorder.layer.mask = gradient

问题:如何用渐变的白色显示相同的文本?

谁能告诉我如何解决这个问题,我已经尝试解决这个问题但还没有结果。

如有任何帮助,我们将不胜感激。

提前致谢。

您需要从顶部开始渐变,因为顶部颜色较深。所以 x 应该有最大的 alpha。然后随着 y 的增加减少 alpha。

试试这个:

let gradient = CAGradientLayer()
gradient.startPoint = CGPoint(x: 1.0, y: 0.0)
gradient.endPoint = CGPoint(x: 0.0, y: 1.0)
let whiteColor = UIColor.black
gradient.colors = [whiteColor.withAlphaComponent(1.0).cgColor, whiteColor.withAlphaComponent(1.0), whiteColor.withAlphaComponent(0.0).cgColor]
gradient.locations = [NSNumber(value: 1.0),NSNumber(value: 0.7),NSNumber(value: 0.0)]
gradient.frame = viewBottomBorder.bounds
viewBottomBorder.layer.mask = gradient

使用上面的代码将渐变应用于您的视图

var gradientBackground : CAGradientLayer?

func applyGradientToBackground() {
    if self.gradientBackground != nil {
        self.gradientBackground?.removeFromSuperlayer()
    }
    self.gradientBackground = CAGradientLayer()
    self.gradientBackground?.frame = self.your_view_name.bounds
    let cor1 = UIColor.black.withAlphaComponent(1).cgColor
    let cor2 = UIColor.white.cgColor
    let arrayColors = [cor1, cor2]
    self.gradientBackground?.colors = arrayColors
    self.your_view_name.layer.insertSublayer(self.gradientBackground!, at: 0)
}

一点选择:

func setupGradient(on view: UIView, withHeight height: CGFloat) {
    // this is view that holds gradient
    let gradientHolderView = UIView()
    gradientHolderView.backgroundColor = .clear
    gradientHolderView.translatesAutoresizingMaskIntoConstraints = false

    // here you set layout programmatically (you can create this view in storyoard as well and attach gradient to it)
    // make sure to position this view under your text
    view.addSubview(gradientHolderView) // alternative: view.insertSubview(gradientHolderView, belowSubview: YourTextLaber)
    gradientHolderView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    gradientHolderView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    gradientHolderView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    gradientHolderView.heightAnchor.constraint(equalToConstant: height).isActive = true

    // this is needed to kinda refresh layout a little so later we can get bounds from view. I'm not sure exactly why but without this if you create view programmatically it is needed
    gradientHolderView.setNeedsLayout()
    gradientHolderView.layoutIfNeeded()

    // here you create gradient as layer and add it as a sublayer on your view (modify colors as you like)
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = gradientHolderView.bounds
    gradientLayer.colors = [UIColor.clear.cgColor, UIColor(white: 0, alpha: 0.6).cgColor]
    gradientHolderView.layer.addSublayer(gradientLayer)
}