IOS:UICollectionView方向上的奇怪动画

时间:2015-03-08 21:40:38

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

UICollectionView的像元大小是全屏大小。用户可以水平滑动。问题是在定位期间,我可以看到下一个或上一个单元格。 (在此处附上截图。红色单元格是当前的单元格,紫色单元格是下一个单元格。)有时在方向之后,集合视图将滚动到错误的单元格索引。 (我使用scrollToItemAtIndexPath强制将其更改为正确的...)

enter image description here      我尝试了两种方法来解决这个问题。

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

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

0 个答案:

没有答案