Swift 4 Uicollectionview Detect End of Scrolling

Swift 4 UICollectionView detect end of scrolling

You may use cellForItem or willDisplayItem methods of collection view. Check if the last cell is being displayed, and load your data. For example:

Swift:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if (indexPath.row == dataSource.count - 1 ) { //it's your last cell
//Load more data & reload your collection view
}
}

Objective-C:

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == dataSource.count - 1 ) { //it's your last cell
//Load more data & reload your collection view

}
}

how to check if which collectionview is scrolled in swift

You can Do it by set tag to CollectionView And Scroll View Delegate method scrollViewDidEndDecelerating Here is the Code :

  1. First Set tag top your CollectionView in your ViewDidLoad method :
firstCollectionView.tag = 1 
secondCollectionView.tag = 2
thirdCollectionView.tag = 3

2.Create three Property Observer variable like this upside of your viewDidload:

   var whichCollectionViewScrolled = "" {
willSet{
print(newValue)
}
}

var isFirstCollectionViewScrolled = false {
willSet{
print("First CollectionView Scrolled : \(newValue)")
}
}
var isSecondCollectionViewScrolled = false {
willSet{
print("Second CollectionView Scrolled : \(newValue)")
}
}
var isthirdCollectionViewScrolled = false {
willSet{
print("Third CollectionView Scrolled : \(newValue)")
}
}

1.lastly inside of your scrollViewDelegate method cast your scrollview instance and check the tag value like this :

extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

if let collectionView = scrollView as? UICollectionView {
switch collectionView.tag {
case 1:
whichCollectionViewScrolled = "First"
isFirstCollectionViewScrolled = true
isSecondCollectionViewScrolled = false
isthirdCollectionViewScrolled = false
case 2:
whichCollectionViewScrolled = "second"
isFirstCollectionViewScrolled = false
isSecondCollectionViewScrolled = true
isthirdCollectionViewScrolled = false
case 3:
whichCollectionViewScrolled = "Third"
isFirstCollectionViewScrolled = false
isSecondCollectionViewScrolled = false
isthirdCollectionViewScrolled = true
default:
whichCollectionViewScrolled = "unknown"
}

} else{
print("cant cast")
}
}

Hope it will help You .

Scrolling CollectionView to left after ending should open another View Controller & same functionality on right end

I've solved it in this way:-

    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
let contentOffsetX = scrollView.contentOffset.x

for cell in self.qrCodeCollctionView.visibleCells {
let indexPath = self.qrCodeCollctionView.indexPath(for: cell)

if indexPath?.row == 0{
if contentOffsetX < -50{
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
self.navigationController?.pushViewController(vc, animated: true)
}
}
else if indexPath?.row == (self.qrCodeDataArray.count - 1){
if contentOffsetX > (scrollView.contentSize.width - scrollView.bounds.width) - 20{
print("over right")
self.scrollViewDidEndDecelerating(self.qrCodeCollctionView)
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
let navController = UINavigationController(rootViewController: vc)

vc.ContactFromVC = self.currentContact
vc.currentIndexVC = self.currentIndex
vc.delegateContact = self
self.present(navController, animated: true, completion: nil)
}
}
}

}

UICollectionView: how to detect when scrolling has stopped

NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView

UICollectionView is a subclass of UIScrollView. So if you have set the delegate and implemented UIScrollViewDelegate, you should be able to detect this the same way as UIScrollView.

For eg:-

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

As per documentation, the above method should tell when the scroll view has ended decelerating the scrolling movement.

How to detect scrolling to an end of an horizontal UICollectionView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.contentOffset.x;
CGFloat contentWidth = scrollView.frame.size.width;

NSLog(@"offset X %.0f", offsetX);
// If your UICollectionView has 3 cell, the contentWidth is 320,
// When you scroll from beginning to end of the UICollectionView, it will print:
// offset X 0
// offset X 1
// offset X 2
// ......
// offset X 320
// offset X 640
}

How can I detect the scroll direction from the UICollectionView?

Try this:

Add this somewhere in you header:

@property (nonatomic) CGFloat lastContentOffset;

Then override the scrollViewDidScroll: method:

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.lastContentOffset > scrollView.contentOffset.y)
{
NSLog(@"Scrolling Up");
}
else if (self.lastContentOffset < scrollView.contentOffset.y)
{
NSLog(@"Scrolling Down");
}

self.lastContentOffset = scrollView.contentOffset.y;
}

Found in Finding the direction of scrolling in a UIScrollView?



Related Topics



Leave a reply



Submit