ios - 如何在 collectionView 之上使用 UIView,以便当我滚动单元格时,UIView 不应该移动?

标签 ios swift uicollectionview

我正在使用 collectionView Cells 创建水平旋转木马,我想在 collectionView 顶部应用一个带有标签的自定义 View ,这样当我滚动单元格时,UIView 应该保留但更改标签和单元格的文本应该改变。请帮助我..

最佳答案

希望这就是您所期待的。

enter image description here

为了演示目的以编程方式完成所有操作。这就是 CollectionViewController 和整个代码的样子。

//
//  SliderController.swift
//  AssignmentSO
//
//  Created by Chanaka Caldera on 7/5/19.
//  Copyright © 2019 StackOverflow. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class SliderController: UICollectionViewController {

    var topview: UIView!
    var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        //MARK: - set up top view and label
        topview = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
        topview.backgroundColor = .green
        view.addSubview(topview)

        label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
        label.text = "Title 0.0"
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 40)
        topview.addSubview(label)


        // MARK: - Registering the cell and set EdgeInsets (if you are using storyboard set EdgeInset would be enough
        self.collectionView!.register(CustomCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        self.collectionView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
        self.collectionView.isPagingEnabled = true

    }
}

// MARK: Datasource
extension SliderController {

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 5
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CustomCell
        cell.label.text = "Cell \(indexPath.row)"
        return cell
    }
}

extension SliderController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let height = UIScreen.main.bounds.height - 100
        let width = UIScreen.main.bounds.width
        return CGSize(width: width, height: height)
    }
}

extension SliderController {
    override func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let xvalue = scrollView.contentOffset.x
        let width = UIScreen.main.bounds.width

        let cellIndex = xvalue/width

        switch cellIndex {
        case 0:
            label.text = "Title \(cellIndex)"
        case 1:
            label.text = "Title \(cellIndex)"
        case 2:
            label.text = "Title \(cellIndex)"
        case 3:
            label.text = "Title \(cellIndex)"
        case 4:
            label.text = "Title \(cellIndex)"
        default:
            label.text = ""
        }
    }
}


//MARK: Custom cell
class CustomCell: UICollectionViewCell {


    var label: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .lightGray
        label = UILabel(frame: CGRect(x: 0, y: 0, width: contentView.bounds.size.width, height: contentView.bounds.size.height))
        label.font = UIFont.systemFont(ofSize: 40)
        label.text = "2"
        label.textAlignment = .center
        addSubview(label)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

希望这对某人有所帮助,并出于演示目的在同一个文件上做了所有事情。对不起。干杯!

关于ios - 如何在 collectionView 之上使用 UIView,以便当我滚动单元格时,UIView 不应该移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56896424/

相关文章:

Swift、OpenGL 和 glVertexAttribPointer

ios - 带有粘性水平指示器的 UICollectionView Swift 3.0

ios - 导航回来时 UICollection View 没有重新加载?

ios - Soundcloud Player 在 Mobile/iOS 上锁定

ios - 模态显示第二个 ViewController 后删除 ViewController

ios - “stringByAppendingPathComponent”不可用

iOS CollectionView header 不显示

iphone - 如何将选项(到 didFinishLaunchingWithOptions)传递到我从调整中激活的应用程序?

ios - 创建一个与二维数组数据结构一起使用的新 IndexPath(使用 Swift 进行应用程序开发,项目 EmojiDictionary,第 556 页)

ios - UITableView 中所有单元格的模糊效果背景