Ios 按单元格宽度的间隔调整UICollectionView滚动偏移

Ios 按单元格宽度的间隔调整UICollectionView滚动偏移,ios,objective-c,Ios,Objective C,我有这样一个集合视图(横向滚动): 当用户滚动时,我想调整表格,使其在静态正方形周围完美排列 我决定在scrollView结束拖动时使用它来执行此操作。然而,它总是有一点,但关闭或在最后完全关闭 我的计算哪里出错了 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ if (![scrollView isKindOfClass:[IndexedColle

我有这样一个集合视图(横向滚动):

当用户滚动时,我想调整表格,使其在静态正方形周围完美排列

我决定在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);
}

您是否使用流布局?如果是,该对象上的其他插图/线间距等是什么?你可能也需要考虑这些因素。我是。我想如果我只考虑contentSize,就可以解释这一点