ios - 自动无限 collectionView - 闪烁

标签 ios iphone objective-c uicollectionview infinite-scroll

我检查了一些解决 UICollectionView 中无限滚动问题的解决方案,但没有一个能完美地工作(123 甚至教程 - 4)。 主要问题在于 UICollectionView 包含图像。当我滚动到第一个或最后一个项目(使其无限)时,在它们加载之前我可以看到一个非常短的“闪烁”,必须将其删除。我不知道如何解决它,欢迎任何帮助。

我应该提一下,我正在制作的画廊不应该允许任何用户交互,它应该自己滚动(可以说是演示文稿)。

我尝试过的两种最有趣的方法:

方法一:collectionView中的14个item,顺序为:@[8, 9, @[0-9], 0, 1](所以8,9,1,2是重复的)

// a timer to move everything
_timer = [NSTimer scheduledTimerWithTimeInterval:0.02f target:self selector:@selector(scroll) userInfo:nil repeats:YES];
// timer selector
- (void)scroll {
    self.collectionView.contentOffset = CGPointMake(self.collectionView.contentOffset.x+1, self.collectionView.contentOffset.y);
}

// check if we need to change offset
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"something" forIndexPath:indexPath];
    if ([self scrollProperly]) {
        //        return cell;
    }
}

// method to check & set proper offset (author: D33pN16h7)
- (BOOL)scrollProperly {
    // check if near the end or beginning
    BOOL test = NO;
    CGFloat currentOffsetX = self.collectionView.contentOffset.x;
    CGFloat currentOffSetY = self.collectionView.contentOffset.y;
    CGFloat contentWidth = self.collectionView.contentSize.width;

    if (currentOffsetX < (contentWidth / 6.0f)) {
        self.collectionView.contentOffset = CGPointMake((currentOffsetX + (contentWidth/2)), currentOffSetY);
        test = YES;
    }
    if (currentOffsetX > ((contentWidth * 4)/ 6.0f)) {
        self.collectionView.contentOffset = CGPointMake((currentOffsetX - (contentWidth/2)), currentOffSetY);
        test = YES;
    }
    return test;
}

第二种方法:collectionView 中的 20 个项目,顺序为:@[@[0-9],@[0-9]](所以整个数组是重复的)。还有定时器,和前面的方法一样。而不是在 collectionView:cellForItemAtIndexPath: 中运行 [self scrollProperly] 我们在这里使用:

if (indexPath.row == 2) {
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:([collectionView numberOfItemsInSection:indexPath.section] - 3) inSection:indexPath.section] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
} else if (indexPath.row == ([collectionView numberOfItemsInSection:indexPath.section] - 1)) {
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:indexPath.section] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
}

最佳答案

好吧,我的最终解决方案是让滚动条变得非常长 - 在 collectionView:cellForItemAtIndexPath: 中,我正在从我的数据库中以其大小为模进行读取。在 collectionView:numberOfItemsInSection: 中有一个常量大小。当我达到恒定大小时,我滚动到第一行。因为我已经使大小不变,所以大量闪烁不会刺激用户,而且滚动实际上是无限的。该解决方案并不完美,但我不知道如何修复它。

关于ios - 自动无限 collectionView - 闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20189877/

相关文章:

ios - NSLayoutConstraint 获得真正的值(value)

ios - 在iOS应用程序和Web应用程序上链接Facebook Graph

iOS 5.1 CoreLocation,位置服务图标不消失

ios - 从 iPhone 安全地发送帖子数据

iphone - 为越狱的 iPhone 创建调整/应用程序/脚本

iphone - 更改了iPhone应用程序目标名称,现在应用程序“运行完成”

iphone - 在 LoadView 中居中 UIView

ios - Apple 文件应用程序 "Raname"文件/文件夹功能在文件提供程序扩展中不起作用

iphone - 我应该如何保护 iOS 中的 SQLite 数据库?

ios - 使用 GCDWebServer 将 UIImage 发送到 LocalwebServer