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))
}
}
としてscrollviewから、あなたのページを取得する必要がありますよりも、現在のページにしたい場合は? – nathan
問題の原因となっているコードには何も書かれていません。より多くのコードを含めるか、自分でデバッグしてデバッグ情報を追加する必要があります。 – Fogmeister
'collectionView'がページあたり4個のセルを表示するようにしたいのですが、正しく動作していますが、5〜7個のセルがある場合、次のページがログに表示されません。最初のセル。理想的には、私は7つのオブジェクトを持っているならば3つのセルを見たいと思っています。 – Christian