IOS:UICollectionView方向上奇怪的动画

UICollectionView的单元大小是全屏大小。 用户可以水平滑动。 问题是在定位期间,我可以看到下一个或上一个单元格。 (这里附加了截图,红色单元格是当前的单元格,紫色单元格是下一个单元格。)有时在方向之后,集合视图会滚动到错误的单元格索引。 (我使用scrollToItemAtIndexPath强制改回它来纠正一个......)

在这里输入图像描述 我尝试了两种方法来解决这个问题。

1)使用流程布局:

- (UICollectionViewLayoutAttributes*)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    // after orientation, scroll to the correct position.
    UICollectionViewLayoutAttributes* attr = [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
     [self.collectionView scrollToItemAtIndexPath:itemIndexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    return attr;
}

- (void)prepareForAnimatedBoundsChange:(CGRect)oldBounds
 {
    NSLog(@"%@ prepare animated bounds change", self);
    [super prepareForAnimatedBoundsChange:oldBounds];
 }

- (void)finalizeAnimatedBoundsChange 
{
    NSLog(@"%@ finalize animated bounds change", self);
    [super finalizeAnimatedBoundsChange];
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
   CGRect oldBounds = self.collectionView.bounds;
   if (!CGSizeEqualToSize(oldBounds.size, newBounds.size)) {
       if(oldBounds.size.width > newBounds.size.width)
       {
           // vertical cell size
           self.itemSize = CGSizeMake(768, 968);
        }
    else{
        // horizontal cell size
        self.itemSize = CGSizeMake(1024, 712);
    }

    NSLog(@"%@ should invalidate layout", self);
    return YES;
}
    return NO;
}

2)在委托调用中使用

 // used in will rotate and did rotate, but both does not work well...
 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
 {
    // do invalidate layout
    [self.CollectionView.collectionViewLayout invalidateLayout];
 }

  // then change the cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

  UIInterfaceOrientation o = [[UIApplication sharedApplication]  statusBarOrientation];

  if (UIInterfaceOrientationIsPortrait(o)) {
       return CGSizeMake(768, 968);
   } else {
      return CGSizeMake(1024, 712);
   }
}

我不想在动画过程中使用假图像来隐藏布局更改。 谢谢!! :)

链接地址: http://www.djcxy.com/p/84165.html

上一篇: IOS: Weird animation on UICollectionView orientation

下一篇: How do I create a UICollectionView with column and row headers?