SelfSizingScroller
修复了 Self Sizing Cells 的 scrollToRow/ItemAtIndexPath:
UITableView 和 UICollectionView 中的自适应大小单元格很棒,但它们使得 scrollToRowAtIndexPath:
和 scrollToItemAtIndexPath:
无法可靠地工作。
这个小库为 UITableView 和 UICollectionView 添加了 reallyScrollTo...
变体,通过使用 CADisplayLink 自身进行滚动,并在需要时进行调整,从而可以可靠地滚动到正确的位置。
针对 UITableView
UITableView.reallyScrollToRow(at indexPath: IndexPath, at scrollPosition: UITableView.ScrollPosition, animated: Bool)
针对 UICollectionView
func reallyScrollToItem(at indexPath: IndexPath, at scrollPosition: UICollectionView.ScrollPosition, animated: Bool)
可扩展的
实际的滚动和确定滚动位置是分离的,因此通过实现协议中的一个方法很容易进行扩展。
protocol ScrollOffsetProviderProtocol {
func targetScrollOffset(witStartOffset startOffset: CGPoint) -> CGPoint?
}
然后调用
class MyScrollOffsetProvider: ScrollOffsetProviderProtocol {
func targetScrollOffset(witStartOffset startOffset: CGPoint) -> CGPoint? {
// calculate actual position, gets called on every animation frame.
// It's fine to return different positions on each invocation,
// as long as it eventually stabilizes.
return ...;
}
}
let scrollView = UIScrollView()
scrollView.scrollTo
只需动态返回要滚动的偏移量,库将会完成。