Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aguilarpgc/e6cb61d8141fb86147d5a70d74b9ee16 to your computer and use it in GitHub Desktop.

Select an option

Save aguilarpgc/e6cb61d8141fb86147d5a70d74b9ee16 to your computer and use it in GitHub Desktop.
Get cell percent visibility inside its collection view
//
// Extension+UICollectionViewCell.swift
// @aguilarpgc
//
// Created by aguilarpgc on 5/07/21.
//
import UIKit
extension UICollectionView {
func percentVisibility(of cell: UICollectionViewCell) -> CGFloat {
guard let indexPathForCell = self.indexPath(for: cell),
let layoutAttributes = self.layoutAttributesForItem(at: indexPathForCell) else {
return CGFloat.leastNonzeroMagnitude
}
let cellFrameInSuper = self.convert(layoutAttributes.frame, to: self.superview)
let interSectionRect = cellFrameInSuper.intersection(self.frame)
let percentOfIntersection: CGFloat = (interSectionRect.height * interSectionRect.width) /
(cellFrameInSuper.height * cellFrameInSuper.width)
return percentOfIntersection
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment