UICollectionView 与屏幕旋转 问题

原来的网址:http://www.itstrike.cn/Question/c69f0dc5-ce17-4c43-91f4-63259ab95d93.html


我有一个使用照片库视图 UICollectionView 与 UICollectionViewFlowLayout ,它有 pagingEnabled 和滚动水平显示一次只有一个视图。

直到我试着旋转它非常适合......

当我在旋转设备, willRotateToInterfaceOrientation:duration: 更新 collectionView.contentOffset 所以它停留在正确的项目上,所以它在动画演示成新尺寸调整大小 currentCell.的问题是在动画中的两个国家之间,'以前' 方向进行动画处理从左上角和扔到视图中的其他单元格。什么是我做错了这样正在进行动画处理从屏幕上的视图是福吧?

在这里是什么它看起来像在行动中:

http://www.smugmug.com/gallery/n-3F9kD/i-BwzRzRf/A(忽略视频不连贯,这是 Quicktime 的错: p)

这里是我 willAnimateRotationToInterfaceOrientation:duration :

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

// Update the flowLayout's size to the new orientation's size
UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
    flow.itemSize = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
} else {
    flow.itemSize = CGSizeMake(self.collectionView.frame.size.width, self.collectionView.frame.size.height);
}
self.collectionView.collectionViewLayout = flow;
[self.collectionView.collectionViewLayout invalidateLayout];

// Get the currently visible cell
PreviewCellView *currentCell = (PreviewCellView*)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:_currentIndex inSection:0]];

// Resize the currently index to the new flow's itemSize
CGRect frame = currentCell.frame;
frame.size = flow.itemSize;
currentCell.frame = frame;

// Keep the collection view centered by updating the content offset
CGPoint newContentOffset = CGPointMake(_currentIndex * frame.size.width, 0);
self.collectionView.contentOffset = newContentOffset;
}

据我所知我找不到任何地方任何示例代码说明了如何使优雅地旋转照片库样式集合视图。

解决方法 1:

我挣扎着与此相当一段时间,直到至少发现了此 '化妆品的解决方法:

在旋转过程中添加全屏 UIImageView 与当前图像 (和正确的自动布局设置的约束) 在之间导航。就像这样:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration 
{

[self.collectionView.collectionViewLayout invalidateLayout];

    // show a UIImageView with the current image on top of the collectionView 
    // to cover the ugly animation
    self.imageViewOnTopOfCollectionView.image = [self imageForItemAtIndexPath:self.currentIndexPath];
    self.imageViewOnTopOfCollectionView.hidden = NO;

    // show a centered, very large 'fakeBackground' view on top of
    // the UICollectionView, but below the UIImageView
    self.fakeBackground.hidden = NO;
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    // ... set correct contentOffset

    // hide the fake views again
    self.imageViewOnTopOfCollectionView.hidden = YES;
    self.fakeBackground.hidden = YES;
}

大的 'fakeBackground' 会防止部分是可见此 imageViews 框架外,而它旋转的丑陋集合视图动画的额外改进。例如超大 (大于所有维度中的视图的边界) UIView 与之间导航,带有 z-索引只之间导航和 imageView 之间的背景颜色相同。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值