t如何在collectionView中获取中心单元格并在滚动时调

t如何在collectionView中获取中心单元格并在滚动时调

本文介绍了Swift如何在collectionView中获取中心单元格并在滚动时调整其大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个水平collectionView,我想对其进行滚动处理时,中心单元会变大.因此,每当一个单元格成为中心单元格时,它就会比其他单元格更大.检查下面的图像.

I have a horizontal collectionView and i would like to manage that when scrolling it, the centre cell becomes larger. So, each time a cell is the center cell it will be larger than the others. Check the image beneath.

有人能以最好的方式帮助我吗?预先感谢!

Can anyone help me with the best way to do this? Thanks in advance!

到目前为止,我的代码:

My code so far:

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource,      UICollectionViewDelegate {
@IBOutlet var horizontalCollectionView: UICollectionView!
@IBOutlet var verticalCollectionView: UICollectionView!

var horizontal = ["1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10"]
var horizontalCellSize = CGSize(width: 50, height: 50)
var horizontalLargeCellSize = CGSize(width: 80, height: 80)


var centerCell = UICollectionViewCell()
var cellIsInCenter = false
var myIndexPath = IndexPath()

override func viewDidLoad() {
    super.viewDidLoad()

}

// When the main view's dimensions change this will re-layout the collection view
override func viewWillLayoutSubviews() {
    verticalCollectionView.collectionViewLayout.invalidateLayout()
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if collectionView == horizontalCollectionView {
        let horizontalCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HorizontalCell", for: indexPath) as! HorizontalCell
        horizontalCell.backgroundColor = UIColor.blue
        horizontalCell.textLabel.text = horizontal[indexPath.row]
        return horizontalCell
    }

   else {

        let verticalCell = collectionView.dequeueReusableCell(withReuseIdentifier: "VerticalCell", for: indexPath) as! VerticalCell
        verticalCell.backgroundColor = UIColor.red
        verticalCell.textLabel.text = horizontal[indexPath.row]
        return verticalCell
    }
}

// MARK: UIScrollViewDelegate

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView == self.horizontalCollectionView {
        let horizontalScrollPosition = scrollView.contentOffset.x * 35
        self.verticalCollectionView.delegate = nil
        self.verticalCollectionView.contentOffset = CGPoint(x: 0, y: horizontalScrollPosition)
        self.verticalCollectionView.delegate = self
    }

    if scrollView == self.verticalCollectionView {
        let verticalScrollPosition = scrollView.contentOffset.y
        self.horizontalCollectionView.delegate = nil
        self.horizontalCollectionView.contentOffset = CGPoint(x: verticalScrollPosition, y: 0)
        self.horizontalCollectionView.delegate = self
    }
}


// MARK: CollectionViewLayout

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    if collectionView == verticalCollectionView {
        let size = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)

        return size
    }

    if indexPath.item == 3 {
        return CGSize(width: 70, height: 70)
    }
    return CGSize(width: 50, height: 50)

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    self.verticalCollectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
    print("\(indexPath)")
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    var closestCell: UICollectionViewCell = self.horizontalCollectionView.visibleCells[0]
    for cell in self.horizontalCollectionView.visibleCells as [UICollectionViewCell] {
        let closestCellDelta = abs(closestCell.center.x - self.horizontalCollectionView.bounds.size.width / 2.0 - self.horizontalCollectionView.contentOffset.x)
        let cellDelta = abs(cell.center.x - self.horizontalCollectionView.bounds.size.width/2.0 - self.horizontalCollectionView.contentOffset.x)
        if (cellDelta < closestCellDelta){
            closestCell = cell
            self.centerCell = cell
            self.cellIsInCenter = true
            horizontalCellSize = CGSize(width: 80, height: 50)
        }
    }
    let indexPath = self.horizontalCollectionView.indexPath(for: closestCell)
    self.horizontalCollectionView.scrollToItem(at: indexPath!, at: .centeredHorizontally , animated: true)
    if closestCell.backgroundColor == UIColor.black {
        closestCell.backgroundColor = UIColor.blue
    }
    horizontalCollectionView.reloadItems(at: [self.horizontalCollectionView.indexPath(for: closestCell)!])
    closestCell.backgroundColor = UIColor.black
}
}

推荐答案

尝试一下:

func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if collectionView == verticalCollectionView {
let size = CGSize(width: collectionView.bounds.width, height: collectionView.bounds.height)
        return size
    }
let point = CGPoint(x: self.view.frame.size.width/2.0, y:35 )
//or you can try this code also
//let point = CGPoint(x: UIScreen.main.bounds.width/2, y:35 )
    print(point)
let centerIndex = self.collectionView.indexPathForItemAtPoint(point)
    if indexPath.item == centerIndex {
        return CGSize(width: 70, height: 70)
    }
    return CGSize(width: 50, height: 50)
}

此代码为您提供了位于集合视图中心的项目索引.希望这会有所帮助

This code gives you the index of the item at the center of the collection View.Hope this helps

这篇关于Swift如何在collectionView中获取中心单元格并在滚动时调整其大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:57