以单元格宽度的间隔调整UICollectionView scrollOffset

时间:2013-11-23 20:47:00

标签: ios objective-c

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

enter image description here

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

我决定在结束拖动时使用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);
}

0 个答案:

没有答案