Determine Percentage of Visibility of Horizontal CollectionView Cells on Screen
You are way overthinking this. Forget about layoutAttributes
and all that fancy-pants stuff and just do what you said you wanted to do: find out how much of the cell is onscreen.
The cell is a view. The screen (represented, let's say, by the window) is a view. Intersect them! Look at the intersection of the cell's frame with the window (or whatever), in appropriately converted coordinates, to get the part of the cell that is onscreen, and compare its size with the cell's frame's size. That is your percentage. Now do anything you like that percentage.
In this example, "anything you like" is just to display the percentage.
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let cells = self.collectionView.visibleCells
for cell in cells {
let f = cell.frame
let w = self.view.window!
let rect = w.convert(f, from: cell.superview!)
let inter = rect.intersection(w.bounds)
let ratio = (inter.width * inter.height) / (f.width * f.height)
let rep = (String(Int(ratio * 100)) + "%")
let label = cell.contentView.subviews.first as! UILabel
label.text = rep
}
}
How to determine when a custom UICollectionViewCell is 100% on the screen
This will return an Array of IndexPaths for the fully visible cells:
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
}
@IBAction func test(_ sender: Any) {
let visCells = fullyVisibleCells(self.collectionView)
print(visCells)
}
UICollectionView current visible cell index
The method [collectionView visibleCells] give you all visibleCells array you want. Use it when you want to get
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
for (UICollectionViewCell *cell in [self.mainImageCollection visibleCells]) {
NSIndexPath *indexPath = [self.mainImageCollection indexPathForCell:cell];
NSLog(@"%@",indexPath);
}
}
Update to Swift 5:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in yourCollectionView.visibleCells {
let indexPath = yourCollectionView.indexPath(for: cell)
print(indexPath)
}
}
Check if a collectionViewCell is showing more than 50% on collectionView
You can ask for
@property(nonatomic) CGPoint contentOffset;
of UIScrollView because UICollectionView's inherits from them.
and don't forget your Cell's have a CALayer that knows the visible Rectangle as well.
CGRect visiblerect = cell.layer.visibleRect;
if (visiblerect.size.height > (cell.frame.size.height * 0.5)) {
// do stuff when cell is more visible then half its size
}
Check if cell completely displayed in collectionview
Using followedCollectionView.indexPathsForVisibleItems()
to get visible cells visibleIndexPaths
and check your indexPath
is contained in visibleIndexPaths
or not, before doing anything with cells.
Ref : @anhtu Check whether cell at indexPath is visible on screen UICollectionView
Also from Apple : var visibleCells: [UICollectionViewCell] { get }
. Returns an array of visible cells currently displayed by the collection view.
count which image user is on horizontal collection view, paging enabled
Solution
So when you wanted to know which cell is visible on the screen then
Use property visibleCells, it will gives you array of cells which is currently visible on screen.
Use property indexPathsForVisibleItems, it will gives you array of indexPath of all visible cells.
For info
In UICollectionView
you are using dequeue
property. The dequeue
property works something like this :
let you have N cells and V
cells are possible on the screen then it takes memory of V+1
cell and after scrolling it releases the memory from the early cell and give it to new visible cell.
Hope it helps you.
Auto Select Middle Visible Cell Of Collection View
You can use delegate of UICollectionView (i.e: didHighlightItemAtIndexPath). just make sure to call collection view delegates on your desired time by calling reload function
self.collectionView.reloadData()
and in you collection view delegate just do this
func collectionView(collectionView: UICollectionView, didHighlightItemAtIndexPath indexPath: NSIndexPath){
var cell : UICollectionViewCell = UICollectionViewCell()
self.collectionView.cellForItemAtIndexPath = indexPath
//change highlighted color as of your need
cell.view.backgroundColor = UIColor.init(red: 25, green: 118, blue: 210).cgColor
}
This will highlight you selected item
Best way to check if UITableViewCell is completely visible
You can get the rect of a cell with rectForRowAtIndexPath:
method and compare it with tableview's bounds rect using CGRectContainsRect
function.
Note that this will not instantiate the cell if it is not visible, and thus will be rather fast.
Swift
let cellRect = tableView.rectForRowAtIndexPath(indexPath)
let completelyVisible = tableView.bounds.contains(cellRect)
Obj-C
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
BOOL completelyVisible = CGRectContainsRect(tableView.bounds, cellRect);
Of course this will not regard the table view being clipped by a superview or obscured by another view.
Related Topics
Xcode:How to Set App to Be iPhone Only
Stream Aws S3 Hls Videos in iOS Browsers
How to Apply Audio Effect to a File and Write to Filesystem - iOS
How to Add Navigation Interface After Create a Tab Bar Controller Programmatically (Swift)
How to Pass Params on Timer Selector
Swift 3.0 Multiple Selection with Select All Cell
How to Monitor Battery Level and State Changes Using Swift
Position a Subview on the Edge of a Circular Shaped View
Duet - Merge 2 Videos Side by Side
Determine Percentage of Visibility of Horizontal Collectionview Cells on Screen
iOS Calculate Text Height in Tableview Cell
Canopenurl Not Working in iOS 10
How to Get Nsdate with Millisecond Accuracy
Change Status Bar Text Colour from White iOS 7/Xcode 5
iOS How to Use Uiapplication Launchapplicationwithidentifier Which Is in Private APIs
Memory Leak with "Libbacktracerecording.Dylib" in React Native iOS Application
None of Your Accounts Are a Member, Code Signing Errors After Upgrading to Xcode 8