UICollectionView performBatchUpdates在旋转设备时导致collectionview错位 [英] UICollectionView performBatchUpdates cause collectionview misplace while rotating the device

查看:133
本文介绍了UICollectionView performBatchUpdates在旋转设备时导致collectionview错位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我旋转设备当collectionview performBatchUpdate与reloadItemsAtIndexPaths时,集合视图将被放错地方。但是视图仍将保持不变。

if I rotate the device While the collectionview performBatchUpdate with reloadItemsAtIndexPaths , the collection view will be misplaced. but view will still stay in a constant position.

简单地修复此问题:

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

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

But I dont think this is a good way to solve this issue.

有没有人有更好的想法?

Does anyone got better idea ?

干杯。

这是我的代码:

    - (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];
        }
    }];
}

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

the output: (if I rotated the device during perform update)

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问题。我的建议是在performBatchUpdates完成后设置框架

It seems to be a UICollectionView issue. My suggestion is to set the frame after performBatchUpdates is done

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

这篇关于UICollectionView performBatchUpdates在旋转设备时导致collectionview错位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆