简体   繁体   中英

How to go to next view controller if i am at last page of uicollection view. its horizontal transition and paging is enable

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView.isPagingEnabled = true
}

@IBAction func onBackButtonTap(_ sender: Any) {
    //    if collection view is on first page i have to pop  
    // self.navigationController?.popViewController(animated: true)
    let collectionBounds = self.collectionView.bounds
    let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x - collectionBounds.size.width))
    self.moveCollectionToFrame(contentOffset: contentOffset)

}

@IBAction func onNextButtonTap(_ sender: Any) {
    // if collection view is on last page i have to push to another view controller 
    let collectionBounds = self.collectionView.bounds
    let contentOffset = CGFloat(floor(self.collectionView.contentOffset.x + collectionBounds.size.width))
    self.moveCollectionToFrame(contentOffset: contentOffset)

}

func moveCollectionToFrame(contentOffset : CGFloat) {
    let frame: CGRect = CGRect(x : contentOffset ,y : self.collectionView.contentOffset.y ,width : self.collectionView.frame.width,height : self.collectionView.frame.height)
    self.collectionView.scrollRectToVisible(frame, animated: true)
}

How to go to next view controller if I am at last page of UICollectionView? Its horizontal transition and paging is enabled.

You can check for the current visible index and check if it is the last or first cell. And based on that you can take your action of pushing or popping the view controller

  var currentIndexPath:IndexPath = collectionView.indexPathsForVisibleItems.last!

        if currentIndexPath.row == yourArray.count - 1 {
            print("last row, we'll push")
        }

        if currentIndexPath.row == 0 {
            print("first row, we'll pop")
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM