2013-10-16 49 views
6

如果我旋轉設備雖然collectionview使用reloadItemsAtIndexPaths執行BatchUpdate,但收集視圖將錯位。但觀點仍將保持不變。UICollectionView performBatchUpdates在旋轉設備時導致collectionview錯位

簡單地解決這個問題:

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

這樣的運氣呢? – lupinglade

回答

0

同樣的問題。它看起來像UICollectionView中的一個錯誤。我能找到的唯一的另一個解決方法是在循環後重置集合視圖的框架。

它發生在旋轉過程中的任何批處理更新看來,不只是reloadItemsAtIndexPaths。

0

我通過重新執行集合視圖的框架旋轉後處理這個:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    self.collectionView.frame = self.view.bounds; 
} 
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]; 
     } 
    }]; 
相關問題