gpt4 book ai didi

ios - 如果我位于 uicollectionview 的最后一页,如何转到下一个 View Controller 。其水平转换和分页已启用

转载 作者:行者123 更新时间:2023-11-29 05:55:27 25 4
gpt4 key购买 nike

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

如果我在 UICollectionView 的最后一页,如何转到下一个 View Controller ?其水平转换和分页已启用。

最佳答案

您可以检查当前可见索引并检查它是最后一个还是第一个单元格。在此基础上,您可以采取推送或弹出 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")
}

关于ios - 如果我位于 uicollectionview 的最后一页,如何转到下一个 View Controller 。其水平转换和分页已启用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55217524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com