Created
July 5, 2021 18:59
-
-
Save aguilarpgc/e6cb61d8141fb86147d5a70d74b9ee16 to your computer and use it in GitHub Desktop.
Get cell percent visibility inside its collection view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // 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