在收集视图中添加淡出效果
如下图所示,我想在收集视图的顶部添加淡出效果。这张照片中的列表是Instagram实时广播中存在的功能。我尝试了Cagradientlayer,但没有得到我想要的结果。事先感谢您的帮助。
: https://i.sstatic.net/guj7777.jpg
Instagram实时列表 希望这种效果始终保持在列表的顶部。顺便说一句,我尝试使用Cagradientlayer尝试的事情:
private func setFadeOut() {
let collectionViewGradient = CAGradientLayer()
collectionViewGradient.colors = [
UIColor.gray.withAlphaComponent(0.2).cgColor,
UIColor.gray.withAlphaComponent(0.1).cgColor,
UIColor.clear.cgColor
]
collectionViewGradient.frame = view.bounds
collectionViewGradient.startPoint = CGPoint(x: 0, y: 0)
collectionViewGradient.endPoint = CGPoint(x: 0, y: 1)
collectionView.layer.insertSublayer(collectionViewGradient, at: 0)
}
I want to add a fade effect to the top of the collection view as in the photo below. The list in this photo is a feature that exists in the live broadcast of Instagram. I tried CAGradientLayer but didn't get the result I wanted. Thanks in advance for the help.
Instagram Live List: https://i.sstatic.net/gUj77.jpg
Edit: I want this effect to always stay at the top of the list. Btw, things I've tried with CAGradientLayer:
private func setFadeOut() {
let collectionViewGradient = CAGradientLayer()
collectionViewGradient.colors = [
UIColor.gray.withAlphaComponent(0.2).cgColor,
UIColor.gray.withAlphaComponent(0.1).cgColor,
UIColor.clear.cgColor
]
collectionViewGradient.frame = view.bounds
collectionViewGradient.startPoint = CGPoint(x: 0, y: 0)
collectionViewGradient.endPoint = CGPoint(x: 0, y: 1)
collectionView.layer.insertSublayer(collectionViewGradient, at: 0)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有正确配置梯度。您的颜色顺序不正确,您缺少位置。
尝试以下操作:
You didn't configured the gradient properly. Your color order was incorrect and you are missing locations.
Try this:
您可以尝试使用下面的视图类。
将梯度视图放置在任何位置(顶部,底部),设置其框架,并确保Z订购还可以(如果需要,将其放在前面)。
距离
参数确定点中的过渡区域。一个示例 - 如果将其放在顶部并将其框架高度设置为150,并且
距离
为100,则梯度将超过100点,其余50点在顶部将是纯黑色(或您设置的任何颜色)。通常,您要放置它,以使其在安全区域过渡,其余不安全的区域是纯黑色(凹口等)。
查看实例需要以编程方式创建,但这无论如何您似乎都在做。对于接口构建器的使用,修改它并不困难。
实例化:
You may try using a view class like the one below.
Place your gradient view wherever you want (top, bottom), set its frame and make sure the z-ordering is ok (bring it to front if needed).
The
distance
argument determines the transition region in points.An example - if you place it at the top and set its frame height to be 150 and
distance
to be 100, the gradient will progress over 100 points and the remaining 50 points on top will be pure black (or whatever color you set it to).Typically you want to place it so that it transitions over the safe area and the remaining unsafe area is pure black (under the notch etc).
The view instance needs to be created programmatically but this is what you seem to be doing anyway. It won't be difficult to modify it for Interface Builder use.
Instantiate with: