iOS - collectionView contentOffset中不准确的CGPoint值

时间:2017-06-16 10:01:28

标签: ios objective-c scroll uicollectionview screen-resolution

我创建了这个带有13个图标的无限collectionView循环,但它在320 x 568分辨率设备上无法正常工作。问题在于在滚动时计算适当的图像内容偏移(它是一个动画 - 它不可触摸)并且它在一段时间后不会居中。这些图像间隔相等,因为我想一次在屏幕上显示3个图标。当3个图标通过previousContentOffset是x = 319.5并且它应该是x = 320.0这个特定情况的解决方法的任何建议?也许迭代单个图标?结果我得到1张图标幻灯片:

  • previousContentOffset =(CGPoint)(x = 106.5,y = 0),

  • width =(double)106.66666666666667,

  • currentContentOffset =(CGPoint)(x = 213.16666666666669,y = 0)

//无限循环

- (void)setupDataForCollectionView {

NSArray *originalArray = self.arrayImages;

NSMutableArray *workingArray = [originalArray mutableCopy];

self.dataArray = [workingArray mutableCopy];
}

// Implementation of infinite loop

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

// calculate width for every device

double contentOffsetWhenFullyScrolledRight = self.collectionViewHorizontal.contentSize.width - self.collectionViewHorizontal.frame.size.width;

if (scrollView.contentOffset.x == contentOffsetWhenFullyScrolledRight) {

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:1 inSection:0];

    [self.collectionViewHorizontal scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

} else if (scrollView.contentOffset.x == 0) {

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForItem:([self.dataArray count] -2) inSection:0];

    [self.collectionViewHorizontal scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
   }
 }

// TimeInterval for animation

- (void)timerAction:(NSTimer *)timer {

//previous icon content offset
CGPoint previousContentOffSet = self.collectionViewHorizontal.contentOffset;

//next icon width
double width = self.collectionViewHorizontal.frame.size.width / 3;
// NSUInteger index = (int)(floor(self.collectionViewHorizontal.contentOffset.x / width) + 1);

// animate icons scroll view
CGPoint currentContentOffset = CGPointMake(previousContentOffSet.x + width, previousContentOffSet.y);

NSIndexPath *indexPath = [self.collectionViewHorizontal indexPathForItemAtPoint:currentContentOffset];
if (indexPath.row > self.dataArray.count - 4) {
    // add elements and reload
    [self.dataArray addObjectsFromArray:[self.arrayImages copy]];
    [self.collectionViewHorizontal reloadData];
}
// sliding animation
[UIView animateWithDuration:.3 delay:.0 usingSpringWithDamping:0.5 initialSpringVelocity:.0 options:kNilOptions animations:^{
    self.collectionViewHorizontal.contentOffset = currentContentOffset;
} completion:^(BOOL finished) {
    [self scrollViewDidEndDecelerating:self.collectionViewHorizontal];
}];
 }

1 个答案:

答案 0 :(得分:0)

我已经实施了一段时间的解决方法,所以我想我应该分享。我决定使用当前图标的索引而不是contentOffset进行迭代。 我创建了一个NSInteger属性currentIndex并添加到timer方法中。

- (void)timerAction:(NSTimer *)timer {

 self.currentIndex++;

 //next icon width
 double width = self.collectionViewHorizontal.frame.size.width / 3;

// animate icons scroll view
CGPoint currentContentOffset = CGPointMake(self.currentIndex * width, 0.0);

NSIndexPath *indexPath = [self.collectionViewHorizontal indexPathForItemAtPoint:currentContentOffset];
if (indexPath.row > (self.dataArray.count /2) {
    // add elements and reload
    [self.dataArray addObjectsFromArray:[self.arrayImages copy]];
    [self.collectionViewHorizontal reloadData];
}
// sliding animation
[UIView animateWithDuration:.3 delay:.0 usingSpringWithDamping:0.5 initialSpringVelocity:.0 options:kNilOptions animations:^{
    self.collectionViewHorizontal.contentOffset = currentContentOffset;
} completion:^(BOOL finished) {
    [self scrollViewDidEndDecelerating:self.collectionViewHorizontal];
}];
 }