2017-04-17 7 views
1

collectionView私は2番目のページで新しいセルをタップすると最初のセルに戻ります。UICollectionViewスクロールの問題が発生しました。

2番目のページでなければなりませんが、私は2番目のページで0.0を取得していますが、1番目のページで0.0を取得しています。

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 

     let pageWidth = collectionCustom.frame.size.width 
     let page = floor((collectionCustom.contentOffset.x - pageWidth/2)/pageWidth) + 1 

     print("Page number: \(page)") 

    } 

私はdidSelectItem方法で起こっては何もないので、なぜ私はその巻物を取得していますか?

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: (inout CGPoint)) { 

     let pageWidth: Float = Float(collectionCustom.frame.width) 
     // width + space 
     let currentOffset: Float = Float(collectionCustom.contentOffset.x) 
     let targetOffset: Float = Float(targetContentOffset.x) 
     var newTargetOffset: Float = 0 
     if targetOffset > currentOffset { 
      newTargetOffset = ceilf(currentOffset/pageWidth) * pageWidth 
     } 
     else { 
      newTargetOffset = floorf(currentOffset/pageWidth) * pageWidth 
     } 
     if newTargetOffset < 0 { 
      newTargetOffset = 0 
     } 
     else if newTargetOffset > Float(collectionCustom.contentSize.width) { 
      newTargetOffset = Float(collectionCustom.contentSize.width) 
     } 

     targetContentOffset.x = CGFloat(currentOffset) 
     collectionCustom.setContentOffset(CGPoint(x: CGFloat(newTargetOffset), y: CGFloat(0)), animated: true) 

     var index: Int = Int(newTargetOffset/pageWidth) 

     var cell: UICollectionViewCell? = collectionCustom.cellForItem(at: IndexPath(item: index, section: 0)) 

     cell = collectionCustom.cellForItem(at: IndexPath(item: index + 1, section: 0)) 

     } 

    } 
+0

としてscrollviewから、あなたのページを取得する必要がありますよりも、現在のページにしたい場合は? – nathan

+0

問題の原因となっているコードには何も書かれていません。より多くのコードを含めるか、自分でデバッグしてデバッグ情報を追加する必要があります。 – Fogmeister

+0

'collectionView'がページあたり4個のセルを表示するようにしたいのですが、正しく動作していますが、5〜7個のセルがある場合、次のページがログに表示されません。最初のセル。理想的には、私は7つのオブジェクトを持っているならば3つのセルを見たいと思っています。 – Christian

答えて

2

あなたは、あなたが経験している行動と望ましい行動のより多くの説明を追加することができ、この

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { 

    let pageNumber = round(scrollView.contentOffset.x/collectionCustom.frame.width) // You can change width according you 
    pageControl.currentPage = Int(pageNumber) 

} 
+0

これは実際に私のために働いています。ありがとう! – Christian

+0

@Christian Welcome mate –

関連する問題