uicollectionview, чтобы посчитать горизонтально, чтобы сделать календарь - PullRequest
0 голосов
/ 10 сентября 2018

как изменить счетчик коллекций с вертикального на горизонтальный, я хочу сделать календарь

это то, что у меня сейчас

enter image description here

это результат, который я ищу

enter image description here

как вы можете видеть, как первое изображение отсчитывается сверху вниз, а второе - слева направо. я хочу перейти ко второму.

вот код

У меня есть 4 отдельных файла для создания календаря с использованием collectionview и пользовательской collectionviewcell

calendarcollection

import UIKit

class calendarCollecction: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! calendarCollecctionCell
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("my number is \(indexPath.row)")
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: self.frame.width, height: self.frame.height)
    }


    lazy var collectionViews: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsetsMake(0,0,0,0)
        layout.scrollDirection = UICollectionViewScrollDirection.horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.showsVerticalScrollIndicator = false
        cv.backgroundColor = UIColor.clear
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews(){
        collectionViews.register(calendarCollecctionCell.self, forCellWithReuseIdentifier: "cell")
        collectionViews.translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = UIColor.clear
        addSubview(collectionViews)
        collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
        collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
        collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
        collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
    }

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

calendarcollectioncell

import UIKit

class calendarCollecctionCell: UICollectionViewCell {
    let label = dateCollection()
    override init(frame: CGRect) {
        super.init(frame: frame)
        framing()
    }

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

    func framing(){
        label.translatesAutoresizingMaskIntoConstraints = false
        addSubview(label)
        label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        label.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        label.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
    }

}

datecollection

import UIKit

class dateCollection: UIView,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
    var dateNumber = 30
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dateNumber
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! dateCollectionCell
        var value = indexPath.row + 1
        cell.label.text = String(value)
        cell.backgroundColor = .red
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("my number is \(indexPath.row)")
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 40, height: 40)
    }

    lazy var collectionViews: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        layout.sectionInset = UIEdgeInsetsMake(5,5,5,5)
        layout.scrollDirection = UICollectionViewScrollDirection.horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.showsVerticalScrollIndicator = false
        cv.showsHorizontalScrollIndicator = false
        cv.backgroundColor = UIColor.clear
        cv.dataSource = self
        cv.delegate = self
        return cv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    func setupViews(){
        collectionViews.register(dateCollectionCell.self, forCellWithReuseIdentifier: "cell")
        collectionViews.translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = UIColor.clear
        addSubview(collectionViews)
        collectionViews.topAnchor.constraint(equalTo: topAnchor, constant: 0).isActive = true
        collectionViews.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 0).isActive = true
        collectionViews.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
        collectionViews.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
        collectionViews.widthAnchor.constraint(equalTo: widthAnchor, constant: 0).isActive = true
    }

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

}

datecollectioncell

import UIKit

class dateCollectionCell: UICollectionViewCell {

    let label = UILabel()
    override init(frame: CGRect) {
        super.init(frame: frame)
        framing()
    }

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

    func framing(){
        label.translatesAutoresizingMaskIntoConstraints = false
        label.textAlignment = .center
        addSubview(label)
        label.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        label.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        label.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        label.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touched "+self.label.text!)
    }

}

1 Ответ

0 голосов
/ 10 сентября 2018

Просто измените scrollDirection для flowLayout

layout.scrollDirection = .vertical

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...