【问题标题】:Gradient Color Swift渐变色 Swift
【发布时间】:2018-12-26 10:22:32
【问题描述】:

这是我想要做的:

截图来自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

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

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

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

  • 显示标签不必使用UIView,直接使用标签即可

标签: ios swift gradient uicolor


【解决方案1】:

您需要从顶部开始渐变,因为顶部较暗。所以 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

【讨论】:

  • 感谢您的回复,但您的解决方案没有被问题解决
  • UIView里面有imageview吗?
【解决方案2】:

一点选择:

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)
}

【讨论】:

  • 感谢您的回复,但您的解决方案没有被问题解决
【解决方案3】:

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

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)
}

【讨论】:

  • 感谢您的回复,但您的解决方案没有被问题解决
  • @Dhiman 取而代之的是取标签并为文本添加阴影。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 2020-04-27
相关资源
最近更新 更多