我有collectionView,我想滚动到下一部分按钮点击。怎么做?
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
@IBAction func nextAction(_ sender: Any) {
//scroll to next
}更新
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
cellOffset = 10
cellWidth = (collectionView.bounds.width / 3) - (cellOffset * 4)
cellHeight = (cellWidth / 2 * 3) + (cellWidth / 2 * 0.65)
return CGSize(width: cellWidth, height: cellHeight)
}发布于 2018-07-18 08:32:33
这可以通过一些简单的步骤来完成:
发布于 2018-07-18 08:31:08
假设您有一个跟踪当前滚动部分的变量。
@IBAction func nextAction(_ sender: Any) {
let attr = collectionView.layoutAttributesForItem(at: IndexPath.init(row: 0, section: currentSection))
collectionView.setContentOffset(attr!.frame.origin, animated: true)
currentSection += 1;
if currentSection >= maxSectionCount {
currentSection = maxSectionCount - 1
}
}发布于 2018-07-18 08:41:54
func goToNextSection() {
guard let nextIndex = getNextSectionIndex() else {
return
}
goToItemAt(nextIndex)
}
func getNextSectionIndex() -> IndexPath? {
guard let firstVisibleCell = mycollection.visibleCells.first, // maybe last in your scenario
let indexOfFirstCell = mycollection.indexPath(for: firstVisibleCell) else {
return nil
}
let nextIndex = IndexPath(item: 0, section: indexOfFirstCell.section + 1)
let yesIhaveCellsHere = true // you need to check if your DS has 'cells' for this, otherwise it will crash at scroll to item
guard yesIhaveCellsHere else {
return nil
}
return nextIndex
}
func goToItemAt(_ index: IndexPath) {
mycollection.scrollToItem(at: index,
at: UICollectionViewScrollPosition.left, // adjust for your scenario
animated: true)
}https://stackoverflow.com/questions/51397031
复制相似问题