iosswiftuiviewcontrolleruicollectionviewuiswipegesturerecognizer

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


I have a collection view in which 4-5 images are there & its scrolling direction is horizontal. I want to push to a view controller after the end of scrolling of both the side (i.e) left & right. If the user is on the most left image of collection view & trying to swipe towards the right again then it should push to a new View Controller & same on the right side. I've tried this so far:-

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    for cell in self.qrCodeCollctionView.visibleCells{
        let indexPaths = self.qrCodeCollctionView.indexPath(for: cell)

        if indexPaths!.row == 0{
            var cellInsets = UIEdgeInsets()
            if #available(iOS 11.0, *) {
                cellInsets = cell.safeAreaInsets
            } else {
                // Fallback on earlier versions
            }
            print(cellInsets)
        }
    }
}

Answers on Swift 4 UICollectionView detect end of scrolling are not according my required functionality.


Solution

  • 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)
                   }
               }
           }
    
    }