0
我创建了一个水平的tableView,其中的单元格占据了整个视图控制器。我不想用默认设置滚动,而是使用scrollView.scrollToItem(at: IndexPath
方法滚动到下一个单元格。自动滚动到下一个/上一个单元格滑动
含义,每次用户向右滚动时,它会自动滚动到下一个单元格,每次用户向左滑动时,它将自动滚动到前一个单元格。每当您滚动一个方向时,它都会自动滚动到下一个或上一个单元格。
我尝试使用scrollViewDidScroll委托方法拦截它,但我遇到了很多问题,它自动滚动来回滚动,并且每小时都会出现一些问题。我究竟做错了什么?
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!你是马特人! –