简体   繁体   English

如果我在uicollection视图的最后一页,如何转到下一个视图控制器。 其水平过渡和分页已启用

[英]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? 如果我在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")
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ios-模拟器开始在水平视图中打开。 如何使它返回默认的人像视图? - ios - simulator started opening in horizontal view. How to get it to go back to default portrait view? 如何强制滚动视图转到下一页? - How do I force a scroll view to go to next page? xcode UICollection查看打开另一个视图控制器 - xcode UICollection View opening another view controller UICollection视图中的自定义视图控制器过渡 - Custom View Controller Transitions From UICollection View iPad中的单个视图控制器中的多个UICOLLECTION View - Multiple UICOLLECTION View in a single view controller in iPad 如何在页面视图中禁用用户滑动,但仍启用与页面视图中子视图的交互。 前按钮 - How to disable user swiping in page view but still enable interaction with the subviews in the page view. e.x. button UITabBar与内容视图重叠。 如何访问内容视图以更新其框架? - UITabBar overlaps with content view. How can I access the content view to update its frame? UICollection视图 - UICollection View 如何实现水平滚动/分页视图控制器? - how to implement horizontal scrolling/paging view controllers? 如何在UICollection视图中添加动画 - How to add animation in UICollection View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM