如何在Uiview中添加另一个Cagradientlayer?

发布于 2025-02-10 09:41:00 字数 707 浏览 2 评论 0原文

我有一个带有可滚动标签的弹出视图(例如Marquee Type)。它在labelview中滚动,只是uiview

我决定使用以下代码在labelview的左侧和右侧添加透明梯度的效果:

    let gradientLayer = CAGradientLayer()
    
    gradientLayer.startPoint = CGPoint(x: 0, y: 0)
    gradientLayer.endPoint = CGPoint(x: 0.5, y: 0)
    gradientLayer.colors = [UIColor.clear.cgColor, UIColor.white.cgColor, UIColor.white.cgColor]
    gradientLayer.locations = [0, 0.2, 1]
    
    gradientLayer.frame = labelView.bounds
    labelView.layer.mask = gradientLayer

但是仅在labelview的左侧可见效果。右侧没有变化:

如何将相同的梯度应用于labelView右侧?

I have a PopupView with a scrollable label (like Marquee type). Its scrolled inside a labelView, which is just a UIView.

I decided to add an effect of transparent gradient to the left and right sides of labelView using the following code:

    let gradientLayer = CAGradientLayer()
    
    gradientLayer.startPoint = CGPoint(x: 0, y: 0)
    gradientLayer.endPoint = CGPoint(x: 0.5, y: 0)
    gradientLayer.colors = [UIColor.clear.cgColor, UIColor.white.cgColor, UIColor.white.cgColor]
    gradientLayer.locations = [0, 0.2, 1]
    
    gradientLayer.frame = labelView.bounds
    labelView.layer.mask = gradientLayer

But the effect is visible only on the left side of the labelView. Right side is unchanged:

How can I apply the same gradient to the labelView right side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2025-02-17 09:41:00

您应该更改起点/终点,

线性梯度位置的颜色位置是呈现[颜色]的相对点。

  0 0.2   0.8 1
  |--|-----|--|

因此,从0到0.2(总长度的20%),我们将具有透明到白色的梯度,然后从0.2(20%)到0.8(80%)从白色到白色到白色(固体白色)和0.8(80) %)至1(100%)从白色到透明颜色的梯度。

来自Apple httpps:// https://develiveer.com.apple.com/doculter.com/documentation /ddocumentation/quartzcorecore/cagradzcore/cagradeirtlayerer/cagradientlayererer/ 1462410-局部

讨论
梯度停止被指定为0到1之间的值。值必须单调增加。如果零,则停止将在整个范围内均匀扩散。默认为nil。

渲染时,颜色会映射到输出颜色空间,然后再插值。

let gradientLayer = CAGradientLayer()

gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
gradientLayer.colors = [UIColor.clear.cgColor, UIColor.white.cgColor, UIColor.white.cgColor, UIColor.clear.cgColor]
gradientLayer.locations = [0, 0.2, 0.8, 1]

gradientLayer.frame = labelView.bounds
labelView.layer.mask = gradientLayer

更新答案

”在此处输入图像说明”

You should change your start/end points and your color locations

In a linear Gradient locations are relative points where [colors] are presented.

  0 0.2   0.8 1
  |--|-----|--|

so from 0 to 0.2 (20% of total length) we will have a gradient of clear to white, then from 0.2 (20%) to 0.8 (80%) a gradient from white to white (solid white) and from 0.8 (80%) to 1 (100%) a gradient from white to clear color.

From apple https://developer.apple.com/documentation/quartzcore/cagradientlayer/1462410-locations

Discussion
The gradient stops are specified as values between 0 and 1. The values must be monotonically increasing. If nil, the stops are spread uniformly across the range. Defaults to nil.

When rendered, the colors are mapped to the output color space before being interpolated.

let gradientLayer = CAGradientLayer()

gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
gradientLayer.colors = [UIColor.clear.cgColor, UIColor.white.cgColor, UIColor.white.cgColor, UIColor.clear.cgColor]
gradientLayer.locations = [0, 0.2, 0.8, 1]

gradientLayer.frame = labelView.bounds
labelView.layer.mask = gradientLayer

After Updated answer

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文