簡體   English   中英

Swift-在效果減半的UICollectionView單元上應用

[英]Swift - Apply effect on the UICollectionView cell cut in half

我的collectionView中有5個單元格,但我只顯示2.5個單元格

例如,在我的collectionView中,我有:[] [] [

“ []”代表我的collectionView中的一個單元格。 我們可以水平滾動以查看下一個單元格。

我想要的是對不透明的最后一個單元格應用某種效果,例如不透明度,以使界面平滑。

我怎樣才能做到這一點 ?

編輯:我想要的示例

在此處輸入圖片說明

但是當切成兩半的單元格完全可見(不再切割)時,透明效果將消失,下一個切成兩半的單元格將具有相同的效果

致以最誠摯的問候,

使用此代碼,我已經對其進行了測試。

//make this variable global
var previousIndexPath : IndexPath = IndexPath()


func scrollViewDidScroll(_ scrollView: UIScrollView) {

    let indexPaths = fullyVisibleCells(collectionView)
    if !previousIndexPath.isEmpty {

        // Reset the Cell with transparent color
        let cell = collectionView.cellForItem(at: previousIndexPath) as! collectionViewCell
        cell.baseView.backgroundColor = .white //Here I used White color 
    }
    if !indexPaths.isEmpty {

        // Highlight the Cell with Desired color
        let cell = collectionView.cellForItem(at: indexPaths.first!) as! collectionViewCell
        cell.baseView.backgroundColor = .red // Here I have used Red color
        previousIndexPath = indexPaths.first!

    }

}

func fullyVisibleCells(_ inCollectionView: UICollectionView) -> [IndexPath] {

    var returnCells = [IndexPath]()

    var vCells = inCollectionView.visibleCells
    vCells = vCells.filter({ cell -> Bool in
        let cellRect = inCollectionView.convert(cell.frame, to: inCollectionView.superview)
        return inCollectionView.frame.contains(cellRect)
    })

    vCells.forEach({
        if let pth = inCollectionView.indexPath(for: $0) {
            returnCells.append(pth)
        }
    })

    return returnCells

}

第一 第二

暫無
暫無

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

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