Ich erstellte eine horizontale TableView, mit Zellen, die den gesamten View-Controller übernehmen. Anstatt mit der Standardeinstellung zu scrollen, möchte ich mit der Methode scrollView.scrollToItem(at: IndexPath
zur nächsten Zelle scrollen.AutoScroll zur nächsten/vorherigen Zelle beim Swipe
Das bedeutet, dass jedes Mal, wenn der Benutzer nach rechts scrollt, automatisch zur nächsten Zelle geblättert wird, und jedes Mal, wenn der Benutzer nach links wischt, wird automatisch zur vorherigen Zelle geblättert. Wenn Sie in eine Richtung blättern, sollte automatisch zur nächsten oder vorherigen Zelle geblättert werden.
Ich habe versucht, es mit ScrollViewDidScroll Delegate-Methode abzufangen, aber ich bin in eine Tonne von Problemen läuft, ist es Autoscrolling hin und her und glitching eine Tonne. Was mache ich falsch?
var previousOffset: CGFloat = 0.0
var allowHorizontalScroll: Bool = true
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (allowHorizontalScroll){
let offset = scrollView.contentOffset.x
let diff = previousOffset - offset
previousOffset = offset
var currentBoardIndex = scrollView.indexPathForItem(at: CGPoint(x:offset, y:10))?.item
if currentBoardIndex != nil {
if diff > 0 {
//print("scroll left")
if (currentBoardIndex != 0){
currentBoardIndex = currentBoardIndex! - 1
allowHorizontalScroll = false
scrollView.scrollToItem(at: IndexPath(row: (currentBoardIndex!), section: 0), at: .left, animated: true)
}
}else{
//print("scroll right")
if (currentBoardIndex != ((boardVCDataSource?.fetchedResultsController.fetchedObjects?.count)! - 1)){
currentBoardIndex = currentBoardIndex! + 1
allowHorizontalScroll = false
scrollView.scrollToItem(at: IndexPath(row: (currentBoardIndex!), section: 0), at: .left, animated: true)
}
}
}
}
}
AH! Du bist der Mann matt! –