【问题标题】:How could I set gradient fixed background on UICollectionView?如何在 UICollectionView 上设置渐变固定背景?
【发布时间】:2014-02-05 10:40:58
【问题描述】:

我使用了以下代码。

CAGradientLayer* collectionRadient = [CAGradientLayer layer];
collectionRadient.bounds = self.collectionView.bounds;
collectionRadient.anchorPoint = CGPointZero;
collectionRadient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor],(id)[endColor CGColor], nil];
[self.collectionView.layer insertSublayer:collectionRadient atIndex:0];

但它绘制在包含图像的单元格上。所以没有显示单元格。

我想在 Cells 下绘制 UICollectionView 的渐变背景,并在视图滚动时修复它。 请告诉我。

【问题讨论】:

    标签: ios objective-c uicollectionview gradient


    【解决方案1】:

    试试这个...你必须分配一个视图才能使用背景视图。

    CAGradientLayer* collectionGradient = [CAGradientLayer layer];
    collectionGradient.bounds = self.view.bounds;
    collectionGradient.anchorPoint = CGPointZero;
    collectionGradient.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor],(id)[[UIColor greenColor] CGColor], nil];
    UIView *vv = [[UIView alloc] init];
    vview.backgroundView = vv;
    [vview.backgroundView.layer insertSublayer:collectionGradient atIndex:0];
    

    【讨论】:

    • 你能解释一下你的vview是什么吗?
    【解决方案2】:

    在 Swift 3.0 中

    我喜欢从自定义渐变类开始

    导入 UIKit @IBDesignable 类渐变视图:UIView { //设置你的开始颜色 @IBInspectable var startColor: UIColor = UIColor.black { didSet { updateColors() }} //设置结束颜色 @IBInspectable var endColor: UIColor = UIColor.white { didSet { updateColors() }} //你可以改变锚点和方向 @IBInspectable var startLocation: Double = 0.05 { didSet { updateLocations() }} @IBInspectable var endLocation: Double = 0.95 { didSet { updateLocations() }} @IBInspectable var Horizo​​ntalMode: Bool = false { didSet { updatePoints() }} @IBInspectable var 对角模式:布尔 = false { didSet { updatePoints() }} 覆盖类 var layerClass: AnyClass { return CAGradientLayer.self } var gradientLayer: CAGradientLayer { 返回图层为! CAGradientLayer } 函数更新点(){ 如果水平模式 { gradientLayer.startPoint = 对角线模式? CGPoint(x: 1, y: 0) : CGPoint(x: 0, y: 0.5) gradientLayer.endPoint = 对角线模式? CGPoint(x: 0, y: 1) : CGPoint(x: 1, y: 0.5) } 别的 { gradientLayer.startPoint = 对角线模式? CGPoint(x: 0, y: 0) : CGPoint(x: 0.5, y: 0) gradientLayer.endPoint = 对角线模式? CGPoint(x: 1, y: 1) : CGPoint(x: 0.5, y: 1) } } 函数更新位置(){ gradientLayer.locations = [startLocation 作为 NSNumber,endLocation 作为 NSNumber] } 函数更新颜色(){ gradientLayer.colors = [startColor.cgColor, endColor.cgColor] } 覆盖 func layoutSubviews() { super.layoutSubviews() 更新点() 更新位置() 更新颜色() } }

    将自定义类添加到项目中的某处并添加到目标中,您只需按照前面提到的方式将其添加为背景视图而不是背景颜色或单独的普通视图发送到后面

    像这样实现:

    让渐变 = GradientView() gradient.frame = self.view.bounds //添加到你的collectionView collectionView?.addSubview(渐变) collectionView?.sendSubview(toBack: 渐变) self.collectionView?.backgroundView = 渐变

    【讨论】:

      【解决方案3】:

      路线

      1. 创建视图
      2. 将该视图分配给您的collectionView的backgroundView
      3. 创建渐变
      4. 将渐变层添加到collectionView backgroundView

      代码 Swift 4.2

        private func addBackgroundGradient() {
          let collectionViewBackgroundView = UIView()
          let gradientLayer = CAGradientLayer()
          gradientLayer.frame.size = view.frame.size
          // Start and end for left to right gradient
          gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
          gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
          gradientLayer.colors = [UIColor.blue.cgColor, UIColor.green.cgColor]
          collectionView.backgroundView = collectionViewBackgroundView
          collectionView.backgroundView?.layer.addSublayer(gradientLayer)
        }
      

      如果您想要一个从上到下的渐变,您只需删除endPointstartPoint。您可以阅读有关渐变的更多信息here

      注意 startPoint 和 endPoint 定义在从(0.0, 0.0)(1.0, 1.0)的坐标平面内

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多