如何在Uiview中添加另一个Cagradientlayer?
我有一个带有可滚动标签的弹出视图(例如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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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-局部
渲染时,颜色会映射到输出颜色空间,然后再插值。
更新答案
You should change your start/end points and your color locations
In a linear Gradient locations are relative points where [colors] are presented.
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
When rendered, the colors are mapped to the output color space before being interpolated.
After Updated answer