我有一个集合视图(横向滚动):
当用户滚动时,我想调整表格,使其在静态方块周围完美排列。
我决定在结束拖动时使用scrollView执行此操作。然而,它总是只是一点点但是关闭或完全关闭。
我的计算错在哪里?
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (![scrollView isKindOfClass:[IndexedCollectionView class]]) return;
IndexedCollectionView *collectionView = (IndexedCollectionView *)scrollView;
NSMutableArray *data = self.sectionsData[collectionView.index];
int cellsCount = data.count;
if (cellsCount==0) {
cellsCount++;
}
//Adjust cells so they line up correctly
CGPoint adjustedOffset = scrollView.contentOffset;
long interval = scrollView.contentSize.width/cellsCount;
long remainder = (long)adjustedOffset.x%interval;
long adjustment = interval-remainder;
adjustedOffset.x +=adjustment;
[UIView animateWithDuration:.3 animations:^{
[scrollView setContentOffset:adjustedOffset];
}];
NSInteger index = collectionView.index;
self.contentOffsetDictionary[[@(index) stringValue]] = @(adjustedOffset.x);
}