【问题标题】:Changing UICollectionView cell image at regular interval定期更改 UICollectionView 单元格图像
【发布时间】:2017-02-15 09:22:15
【问题描述】:

我正在使用 UICollectionView 开发 IOS 10.x 应用程序,并希望定期更改特定单元格的图像。

下面的代码显示了当前的实现。即使它应该每半秒更改一次单元格背景图像,它也会立即更改图像,而忽略 NSThread SleepAt 0.5 秒的间隔。

我怀疑有关主线程处理或 ReloadItem 方法的一些问题,但尚未得出明确的结论。非常欢迎任何见解!谢谢。

NSNumber* originalCardSelected;
int position;

for (int i = 0; i < [opponentOriginalCardArray count]; i++) {

  originalCardSelected = [opponentOriginalCardArray objectAtIndex:i];
  position = [self convertLevelDataPositionToCellViewPosition:[originalCardSelected intValue]];

  NSMutableArray *tmpBackgroundAssets = [self getNewAssetBackgroundBasedOnBackgroundType:playbackBackground Index:position];
    self.backgroundAssets = tmpBackgroundAssets;

    dispatch_async(dispatch_get_main_queue(), ^{



        [collectionViewRoot reloadItemsAtIndexPaths:[collectionViewRoot indexPathsForVisibleItems]];
    });
    [NSThread sleepForTimeInterval:0.5];


}

【问题讨论】:

  • 我认为UIImageView animationImages 属性可能更适合您正在尝试做的事情。
  • dispatch_async 异步执行代码,这就是为什么在 [NSThread sleepForTimeInterval:0.5] 行之前发生重新加载的原因。
  • 更好地使用NSTimer

标签: ios xcode uicollectionview uicollectionviewcell


【解决方案1】:

您应该使用 performBatchUpdates(_:completion:) 方法并在其中添加您的 for 循环。

记住不要在块内保留强引用以避免保留循环。

编辑:

在完成块中,您可以检查是否完成并添加您的 NSThread 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多