如何在滚动Swift时设置UICollectionView图层不移动/静态

时间:2016-04-30 16:52:08

标签: ios swift uicollectionview calayer cagradientlayer

我向UICollectionView添加了渐变图层,但图层设置为屏幕的宽度。我试图在滚动时保持图层静态,但无法找到解决方案。尝试设置到collectionView的框架和currentVC,当我滚动图层时也是滚动。这是我的代码,感谢您的帮助。

let gradinatLayer = CAGradientLayer()
    gradinatLayer.frame = self.collectionView.frame //self.currentViewController.view.frame

    let cgColors:[CGColorRef] = [UIColor.blackColor().colorWithAlphaComponent(0.7).CGColor, UIColor.clearColor().CGColor, UIColor.blackColor().colorWithAlphaComponent(0.7).CGColor]

    gradinatLayer.colors = cgColors
    gradinatLayer.startPoint = CGPointMake(0, 0.5)
    gradinatLayer.endPoint = CGPointMake(1.0, 0.5)
    gradinatLayer.locations = [0.15, 0.50, 0.85]

    self.collectionView.layer.insertSublayer(gradinatLayer, atIndex: 0)

//This doesn't work
self.collectionView.layer.shouldRasterize = true
self.collectionView.layer.rasterizationScale = UIScreen.mainScreen().scale

2 个答案:

答案 0 :(得分:2)

感谢您的所有意见。这是我在深入研究UIView后的解决方案。不幸的是,将super viewssubviews添加到UICollectionView仍然可以保持视图滚动。感谢John Stephen,他展示了如何使用带有子视图的userIntaractions创建透明的UIView,这是可能的。

我在@IBOutlet weak var passThroughView: PassThroughView!之上声明了collectionView

这是现在可以使用的代码:

let gradinatLayer = CAGradientLayer()
    gradinatLayer.frame = self.currentViewController.view.frame

    let cgColors:[CGColorRef] = [UIColor.blackColor().colorWithAlphaComponent(0.7).CGColor, UIColor.clearColor().CGColor, UIColor.blackColor().colorWithAlphaComponent(0.7).CGColor]

    gradinatLayer.colors = cgColors
    gradinatLayer.startPoint = CGPointMake(0, 0.5)
    gradinatLayer.endPoint = CGPointMake(1.0, 0.5)
    gradinatLayer.locations = [0.15, 0.50, 0.85]

    passThroughView.layer.insertSublayer(gradinatLayer, atIndex: 0)

答案 1 :(得分:0)

尝试将图层添加到collectionView的backgroundview中:

self.collectionView.backgroundView.layer.addSublayer(gradinatLayer)