1

I have 5 cells in my collectionView but I only display 2.5 cells

For Example in my collectionView I have this : [][][

'[]' represents one cell in my 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

enter image description here

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,

kirusamma
  • 119
  • 11

1 Answers1

0

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

}

First Second

Vicky_Vignesh
  • 584
  • 2
  • 14
  • 1
    Sorry for my late answer. Thanks but it didn't work for me but i solved this issue. I'am going to post the code. Thanks again ! – kirusamma Jul 19 '19 at 09:58