我正在阅读 Apple关于在集合视图中设置动画更改的指南,试图模仿照片应用程序。选择图像时,图像会从其在 collectionview 中所在的区域“增长”到完整尺寸的视图。
Apple 注意到使用了一个UICollectionViewLayout
对象,但对我来说它看起来非常混乱,而且完整尺寸画廊中的最终图像也不可见,因为动画以集合视图为中心。
我的代码如下
-(void)setHorizontalLayout:(BOOL)layout
{
if (layout == YES)
{
UICollectionViewTransitionLayout *layout =[self.collectionView startInteractiveTransitionToCollectionViewLayout:[self getHorizontalPagingLayout] completion:^(BOOL completed, BOOL finish) {
[self.collectionView setPagingEnabled:YES];
}];
[self.collectionView.collectionViewLayout invalidateLayout];
layout.transitionProgress = 0.1;
double delayInSeconds = 0.1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self updateLayout:layout];
//[self.collectionView.collectionViewLayout invalidateLayout];
//[self.collectionView finishInteractiveTransition];
});
[self.collectionView.collectionViewLayout invalidateLayout];
return;
}
}
-(void)updateLayout:(UICollectionViewTransitionLayout *)layout
{
if (layout.transitionProgress >= 1.0)
{
[self.collectionView finishInteractiveTransition];
return;
}
double delayInSeconds = 0.05;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
layout.transitionProgress += 0.005;
[self.collectionView.collectionViewLayout invalidateLayout];
[self updateLayout:layout];
});
}