简体   繁体   English

Swift-在效果减半的UICollectionView单元上应用

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

I have 5 cells in my collectionView but I only display 2.5 cells 我的collectionView中有5个单元格,但我只显示2.5个单元格

For Example in my collectionView I have this : [][][ 例如,在我的collectionView中,我有:[] [] [

'[]' represents one cell in my collectionView. “ []”代表我的collectionView中的一个单元格。 We can scroll horizontally to see the next cell. 我们可以水平滚动以查看下一个单元格。

What I want is to apply some effect like opacity to the last cell visible cut in half to have a smooth UI. 我想要的是对不透明的最后一个单元格应用某种效果,例如不透明度,以使界面平滑。

How can i do this ? 我怎样才能做到这一点 ?

EDIT: A sample what i want 编辑:我想要的示例

在此处输入图片说明

But when the cell cut in half is totally visible (not cut anymore) the transparent effect will disappear and the next cell cut in half will have the same effect 但是当切成两半的单元格完全可见(不再切割)时,透明效果将消失,下一个切成两半的单元格将具有相同的效果

Thanks Best Regard, 致以最诚挚的问候,

Use this Code, It works I have tested It. 使用此代码,我已经对其进行了测试。

//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