簡體   English   中英

使 CollectionView Cell 水平單元格比其他單元格大

[英]Make CollectionView Cell horizontal cell bigger than others

我有一個 collectionView 並且想讓中心單元格比其他單元格大,當移動到上一個或下一個單元格時,讓它在中心更大

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.collectionView {
        return slidData.count
    } else {
        return categoryData.count
    }
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == self.collectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! SliderImgCell

        cell.categoryName.text = slidData[indexPath.row].imageTitle
        cell.detail.text = slidData[indexPath.row].imageContent
        cell.pics = slidData[indexPath.item]

        return cell
    } else{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "categoryCell", for: indexPath) as! CategoryCell

        cell.categoryName.text = categoryData[indexPath.row].depName
        cell.pics = categoryData[indexPath.item]

        return cell
    }
}

您需要像這樣保存所選的 indexPath:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    selectedIndexPath = indexPath 
}

您的集合視圖必須確認 UICollectionViewDelegateFlowLayout。 然后在 sizeForItemAt 方法中調整單元格的大小:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath == selectedIndexPath {
        return CGSize(width: 200, height: 90)
    } else {
        return CGSize(width: 180, height: 80)
    }
}

基本上你想要一個旋轉木馬動畫。

看看這個庫,要么完整地使用它,要么從代碼中獲取幫助。

您可以通過使用ScrollViewDidScroll(_:)方法並找到中心單元格並放大它來實現此目的。 我對下面的代碼寫了明確的評論,如果你想詳細了解我的文章:

https://medium.com/@sh.soheytizadeh/zoom-uicollectionview-central-cell-swift-5-e63cad9bcd49

func setFocusedCellStyle(_ scrollView: UIScrollView) {
        guard scrollView is UICollectionView else { return }
        // get the centerPoint
        let centerPoint = CGPoint(x: self.collectionView.frame.size.width / 2 + scrollView.contentOffset.x, y: self.collectionView.frame.size.height / 2 + scrollView.contentOffset.y)
        // get the indexPath for the cell at center
        if let indexPath = self.collectionView.indexPathForItem(at: centerPoint), self.centerCell == nil {
            // centerCell is instance variable of type: YourCell
            self.centerCell = (self.colorPickerCollectionView.cellForItem(at: indexPath) as! CustomCollectionViewCell)

            UIView.animate(withDuration: 0.2) {
                // Choose desired scale value
                self.transform = CGAffineTransform(scaleX: 1.58, y: 1.58)
            }
        }

        if let cell = self.centerCell { // center cell is global variable
            let offsetX = centerPoint.x - cell.center.x // get to current position of the cell
            // when cell is 15 pixels away from the center to the sides
            // reset the cell size.
            if offsetX < -15 || offsetX > 15 {
                UIView.animate(withDuration: 0.2) {
                     self.transform = CGAffineTransform.identity
                }
                self.centerCell = nil // set this to nil so next cell in the center will enter the above scope
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM