Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 222 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UICollectionView current visible cell index

#11
Also check this snippet

let isCellVisible = collectionView.visibleCells.map { collectionView.indexPath(for: $0) }.contains(inspectingIndexPath)
Reply

#12
`indexPathsForVisibleItems` might work for most situations, but sometimes it returns an array with more than one index path and it can be tricky figuring out the one you want. In those situations, you can do something like this:

CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];

This works especially well when each item in your collection view takes up the whole screen.

**Swift version**

let visibleRect = CGRect(origin: collectionView.contentOffset, size: collectionView.bounds.size)
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let visibleIndexPath = collectionView.indexPathForItem(at: visiblePoint)
Reply

#13
**Swift 3 & Swift 4:**

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect()

visibleRect.origin = collectionView.contentOffset
visibleRect.size = collectionView.bounds.size

let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

print(indexPath[1])
}
If you want to show actual number than you can add +1
Reply

#14
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)
}
}

Reply

#15
In this thread, There are so many solutions that work fine if **cell takes full screen** but they use collection view bounds and midpoints of Visible rect However there is a simple solution to this problem

DispatchQueue.main.async {
let visibleCell = self.collImages.visibleCells.first
print(self.collImages.indexPath(for: visibleCell))
}
by this, you can get indexPath of the visible cell. I have added DispatchQueue because when you swipe faster and if for a brief moment the next cell is shown then without dispactchQueue you'll get indexPath of briefly shown cell not the cell that is being displayed on the screen.
Reply

#16
**Swift 5**:

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect()

visibleRect.origin = collectionView.contentOffset
visibleRect.size = collectionView.bounds.size

let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)

guard let indexPath = collectionView.indexPathForItem(at: visiblePoint) else { return }

print(indexPath)
}


Working Answers Combined In Swift 2.2 :

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

var visibleRect = CGRect()

visibleRect.origin = self.collectionView.contentOffset
visibleRect.size = self.collectionView.bounds.size

let visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect))

let visibleIndexPath: NSIndexPath = self.collectionView.indexPathForItemAtPoint(visiblePoint)

guard let indexPath = visibleIndexPath else { return }
print(indexPath)

}
Reply

#17


UICollectionView current visible cell index: Swift 3, 4 and 5+
----------------------------------------------------

var visibleCurrentCellIndexPath: IndexPath? {
for cell in self.collectionView.visibleCells {
let indexPath = self.collectionView.indexPath(for: cell)
return indexPath
}

return nil
}

As an Extension:

extension UICollectionView {
var visibleCurrentCellIndexPath: IndexPath? {
for cell in self.visibleCells {
let indexPath = self.indexPath(for: cell)
return indexPath
}

return nil
}
}

Usage:

if let indexPath = collectionView.visibleCurrentCellIndexPath {
/// do something
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through