【发布时间】:2017-09-08 17:46:09
【问题描述】:
我有一个有两种颜色的 CAGradientLayer,黑色和透明。当视图出现时,渐变会正确显示,但是一旦我滚动 CollectionView,渐变的透明部分就会消失。
let gradient: CAGradientLayer = {
let g = CAGradientLayer()
g.colors = [UIColor.black.cgColor, UIColor.clear.cgColor]
g.startPoint = CGPoint(x: 0.5, y: 1)
g.endPoint = CGPoint(x: 0.5, y: 0)
return g
}()
有什么建议吗?
编辑 我已经包含了一些额外的代码,其中包括 UIView 的设置和渐变层的添加。
let toolBar: UIView = {
let n = UIView()
n.translatesAutoresizingMaskIntoConstraints = false
n.backgroundColor = UIColor.clear
return n
}()
view.addSubview(s.toolBar)
view.addConstraintsWithFormat("H:|[v0]|", views: s.toolBar)
s.toolBar.heightAnchor.constraint(equalToConstant: 50).isActive = true
s.toolBar.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
s.toolBar.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
s.toolBar.layer.insertSublayer(s.gradient, at: 0)
s.gradient.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
【问题讨论】:
-
您要将图层添加到哪里?整个集合视图?
-
@FangmingNing 将渐变添加到 UIView 中,其框架是第二张图像中黑条的框架。
-
你能发布添加那个UIView的代码吗?这将为我们提供更好的线索为什么您的图层丢失
-
@FangmingNing 我已将其包含在编辑中。
-
是的,我知道您正确地将渐变图层添加到您的视图中,我们可以从您的第一张照片中批准这一点。您收到此错误的原因是因为视图以某种方式丢失,而不是渐变层。我怀疑这发生在您设置每个单元格的集合视图委托方法中。由于单元格是由原型回收的,因此您最终可能会得到一个没有渐变视图的单元格。我对你有意义吗?
标签: ios swift uicollectionview cagradientlayer