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

时间:2013-10-16 06:16:45

标签: ios uicollectionview

如果我旋转设备当collectionview使用reloadItemsAtIndexPaths执行StickUpdate时,集合视图将被放错地方。但观点仍将保持不变。

简单地解决此问题:

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}}

3 个答案:

答案 0 :(得分:0)

这里的问题相同。它看起来像UICollectionView中的一个错误。我能找到的唯一其他解决方法是在旋转后重置集合视图的框架。

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

答案 1 :(得分:0)

我通过在轮换后重新强制执行集合视图的框架来处理这个问题:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    self.collectionView.frame = self.view.bounds;
}

答案 2 :(得分:0)

这似乎是一个UICollectionView问题。我的建议是在performBatchUpdates完成后设置框架

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