结束后向左滚动CollectionView应在右侧打开另一个View Controller和相同功能

问题描述 投票:-2回答:1

我有一个收藏视图,其中有4-5张图像,并且其滚动方向是水平的。我想在左右两侧滚动结束后推送到视图控制器。如果用户在集合视图的最左侧图像上并试图再次向右滑动,则它应推送到一个新的视图控制器,并在右侧相同。到目前为止,我已经尝试过:-

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

Swift 4 UICollectionView detect end of scrolling上的答案不符合我所需的功能。

ios swift uiviewcontroller uicollectionview uiswipegesturerecognizer
1个回答
0
投票

我已经通过这种方式解决了:-

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

}
© www.soinside.com 2019 - 2024. All rights reserved.