如何在滚动 Swift 时设置 UICollectionView 层不 moving/static
How to set UICollectionView layer not moving/static while scrolling Swift
我给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
尝试将图层添加到 collectionView 的背景视图:
self.collectionView.backgroundView.layer.addSublayer(gradinatLayer)
感谢您的所有评论。这是我深入研究 UIView 后的解决方案。不幸的是,将 super views
和 subviews
添加到 UICollectionView
仍然保持视图可滚动。感谢 John Stephen,他展示了如何使用 userIntaractions 及其子视图创建透明的 UIView,这成为可能。
我在 collectionView
之上声明了一个 @IBOutlet weak var passThroughView: PassThroughView!
。
这是现在有效的代码:
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)
我给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
尝试将图层添加到 collectionView 的背景视图:
self.collectionView.backgroundView.layer.addSublayer(gradinatLayer)
感谢您的所有评论。这是我深入研究 UIView 后的解决方案。不幸的是,将 super views
和 subviews
添加到 UICollectionView
仍然保持视图可滚动。感谢 John Stephen,他展示了如何使用 userIntaractions 及其子视图创建透明的 UIView,这成为可能。
我在 collectionView
之上声明了一个 @IBOutlet weak var passThroughView: PassThroughView!
。
这是现在有效的代码:
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)