Determine Percentage of Visibility of Horizontal Collectionview Cells on Screen

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.

Sample Image

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

  1. Use property visibleCells, it will gives you array of cells which is currently visible on screen.

  2. 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



Leave a reply



Submit