gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:34:11 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com