ios - UICollectionView performBatchUpdates 导致 collectionview 在旋转设备时错位

标签 ios uicollectionview

如果我旋转设备而 collectionview performBatchUpdate 和 reloadItemsAtIndexPaths , Collection View 将错位。但 View 仍将保持不变。

简单地解决这个问题:

setting [UIView setAnimationEnabled: NO]; in willRotateToInterfaceOrientation
+ 
setting [UIView setAnimationEnabled: YES]; in didRotateToInterfaceOrientation

但我认为这不是解决这个问题的好方法。

有没有人有更好的主意?

干杯。

这是我的代码:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    NSLog(@"Before Rotation");
    NSLog(@"collecitonview Rect : %@", NSStringFromCGRect(self.collectionView.frame));
    NSLog(@"view Rect : %@", NSStringFromCGRect(self.view.frame));
}

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    NSLog(@"After Rotation");
    NSLog(@"collecitonview Rect : %@", NSStringFromCGRect(self.collectionView.frame));
    NSLog(@"view Rect : %@", NSStringFromCGRect(self.view.frame));
}

    - (void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    for(int i = 0 ; i < 30; i++){
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
            CGFloat hue = arc4random() % 20;
            [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:hue inSection:0]]];
        }];
        [_operationsArray addObject:operation];
    }
    [self performUpdate];
}

    - (void)performUpdate{

    if(_operationsArray.count ==  0)return;

    [self.collectionView performBatchUpdates:^{
        NSBlockOperation *operation = (NSBlockOperation*)[_operationsArray firstObject];
        [operation start];
    } completion:^(BOOL finished) {
        if(_operationsArray.count !=  0){
            [_operationsArray removeObjectAtIndex:0];
            [self performUpdate];
        }
    }];
}

输出:(如果我在执行更新期间旋转了设备)

2013-10-16 17:05:18.445 collectionviewbatchupdateTest[90419:a0b] Before Rotation
2013-10-16 17:05:18.446 collectionviewbatchupdateTest[90419:a0b] collecitonview Rect : {{0, 0}, {1024, 768}}
2013-10-16 17:05:18.446 collectionviewbatchupdateTest[90419:a0b] view Rect : {{0, 0}, {768, 1024}}
2013-10-16 17:05:18.851 collectionviewbatchupdateTest[90419:a0b] After Rotation
2013-10-16 17:05:18.851 collectionviewbatchupdateTest[90419:a0b] collecitonview Rect : {{-128, 0}, {1024, 1024}}
2013-10-16 17:05:18.852 collectionviewbatchupdateTest[90419:a0b] view Rect : {{0, 0}, {768, 1024}}

最佳答案

同样的问题。它看起来像是 UICollectionView 中的错误。我能找到的唯一其他解决方法是在旋转后重置 Collection View 的框架。

它似乎发生在旋转期间的任何批更新中,而不仅仅是 reloadItemsAtIndexPaths。

关于ios - UICollectionView performBatchUpdates 导致 collectionview 在旋转设备时错位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19396440/

相关文章:

android - HTML5固定尺寸页面

ios - 如何为 CollectionViewCell 设置两个 ImageView

ios - UICollectionView 无法滚动

ios - 如何让 CollectionView 单元格返回 View

ios - Xcode 5 表示测试失败但显示绿色复选标记

ios - 如何将视觉框架坐标系转化为ARKit?

ios - UITableViewCell 中 UIStackView 中的 UICollectionview

ios - 如何使 UICollectionView 反弹来说明可滚动性?

ios - Swift:如何在UITableViewController(包含UICollectionView)中使用 "didSelectItemAtIndexPath"?

IOS6 反向地理编码