在UITableView或UIScrollView中添加alignment位置

是否有可能在UITableView或UIScrollView中添加alignment位置? 我的意思是不自动滚动到一个位置,如果我按下button或调用一些方法来做到这一点,我的意思是如果我滚动我的滚动视图或表视图围绕一个特定的点,如0,30,它会自动捕捉到它并留在那里? 所以如果我的滚动视图或表格视图滚动,然后用户让介入0,25或0,35,它会自动“捕捉”,并在那里滚动? 我可以想象也许可以放入一个if语句来testing该位置是否在UIScrollView的scrollViewDidEndDragging:WillDecelerate:scrollViewWillBeginDecelerating:方法落在该区域,但我不确定如何在UITableView的情况下实现这一点。 任何指导将不胜感激。

像帕万说,scrollViewWillEndDragging:withVelocity:targetContentOffset:是你应该使用的方法。 它适用于表格视图和滚动视图。 下面的代码应该适合你,如果你正在使用一个表视图或垂直滚动​​滚动视图。 44.0是示例代码中表格单元格的高度,因此您需要将该值调整为单元格的高度。 如果用于水平滚动的滚动视图,则将y交换为x,并将44.0更改为滚动视图中各个部分的宽度。

 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { // Determine which table cell the scrolling will stop on. CGFloat cellHeight = 44.0f; NSInteger cellIndex = floor(targetContentOffset->y / cellHeight); // Round to the next cell if the scrolling will stop over halfway to the next cell. if ((targetContentOffset->y - (floor(targetContentOffset->y / cellHeight) * cellHeight)) > cellHeight) { cellIndex++; } // Adjust stopping point to exact beginning of cell. targetContentOffset->y = cellIndex * cellHeight; } 

我强烈build议您使用scrollViewWillEndDragging: withVelocity: targetContentOffset:方法,而不是为了达到您的目的。 将目标内容偏移量设置为您想要的位置。

我也build议你看看已经发布在SO上的重复问题。

看看这些post

在Swift 2.0中,当表格有一个内容插入和简化的事情时, ninefifteen的很好的答案变成:

 func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { let cellHeight = 44 let y = targetContentOffset.memory.y + scrollView.contentInset.top + cellHeight / 2 var cellIndex = floor(y / cellHeight) targetContentOffset.memory.y = cellIndex * cellHeight - scrollView.contentInset.top; } 

通过添加cellHeight / 2 ,九十五的if -statement来增加索引不再需要。

如果你真的必须手动这样做,这是一个Swift3版本。 但是,强烈build议只打开UITableView的分页,这已经为您处理。

 let cellHeight = 139 func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { targetContentOffset.pointee.y = round(targetContentOffset.pointee.y / cellHeight) * cellHeight } // Or simply self.tableView.isPagingEnabled = true 

这是Swift 2.0的等价物

 func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { let cellWidth = CGRectGetWidth(frame) / 7 // 7 days var index = round(targetContentOffset.memory.x / cellWidth) targetContentOffset.memory.x = index * cellWidth } 

这个复杂的四舍五入是不需要的,只要你用round而不是floor

我相信如果你使用委托方法:

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSLog(@"%f",scrollView.contentOffset.y); if (scrollView.contentOffset.y > 350 && scrollView.contentOffset.y < 370) { NSLog(@"setContentOffset"); [scrollView setContentOffset:CGPointMake(0, 350) animated:YES]; } } 

玩它,直到你得到所需的行为。 也许计算下一个tableViewCell / UIView的下一个上边缘,然后在最近的一个顶端停下来。

原因是: if (scrollView.contentOffset.y > 350 && scrollView.contentOffset.y < 370)是滚动视图调用scrollViewDidScroll以快速跳转,所以我给了一个介于if之间。

您也可以通过以下方式知道速度正在减慢:

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@"%f",scrollView.contentOffset.y); int scrollSpeed = abs(scrollView.contentOffset.y - previousScrollViewYOffset); previousTableViewYOffset = scrollView.contentOffset.y; if (scrollView.contentOffset.y > 350 && scrollView.contentOffset.y < 370 && scrollSpeed < minSpeedToStop) { NSLog(@"setContentOffset"); [scrollView setContentOffset:CGPointMake(0, 350) animated:YES]; } }