[英]Swift - Apply effect on the UICollectionView cell cut in half
使用此代码,我已经对其进行了测试。
//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.