ios - 如果我在 uicollection 视图的最后一页,如何转到下一个视图控制器。它的水平转换和分页启用
问题描述
@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 的最后一页,如何转到下一个视图控制器?它的水平过渡和分页已启用。
解决方案
您可以检查当前可见索引并检查它是最后一个还是第一个单元格。基于此,您可以采取推送或弹出视图控制器的操作
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")
}
推荐阅读
- angular - 刷新页面时我丢失了有关用户的信息?Angular 2+ NgRx 商店
- java - API 上 GET 调用的 403 状态
- java - Rotating Rectangle with mouse isn't consistant
- java - 获取 org.json.JSONEXCEPTION:解码令牌时字符处的未确定对象
- laravel - Laravel 雄辩的关系与数据透视表
- database-normalization - 标准化表格
- sqlite - 如何从配置文件中获取路径
- javascript - Javascript 从服务器获取会话 cookie
- r - 如何从来自多个 json 页面的多个列表中提取值
- javascript - 由于 lodash 在 Vue 中使用 Jest 进行测试在子组件中失败