弹出和缩放集合视图单元格 - UICollectionView单元格动画

3
我正在尝试使用我的集合视图选择创建一个酷炫的动画。基本上,我有一个显示照片单元格的集合视图。我想要发生的是,将单元格从其位置弹出,缩放并移动到屏幕中心,在那里用户将被提示确认选择。
这是我的代码,但目前动画采用单元格的原始框架位置,因此如果我滚动,它不会考虑到框架位置已不再相同。
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

PhotoCell *cell = (PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
collectionView.allowsSelection = NO;
[self createAnimationWithCell:cell];

}

- (void)createAnimationWithCell:(PhotoCell *)cell {

UIImageView *selectedImage = [[UIImageView alloc] initWithFrame:cell.bounds];
selectedImage.center = cell.center;
selectedImage.image = cell.imageView.image;
[self.view addSubview:selectedImage];

[UIView animateWithDuration:2.5 animations:^{
    selectedImage.center = self.view.center;
} completion:^(BOOL finished) {
    [selectedImage removeFromSuperview];
    self.collectionView.allowsSelection = YES;
}];
}
1个回答

2
我自己解决了:
对于那些遇到同样问题的人,我们必须考虑到集合视图已经滚动了多少(偏移量),并使用这些信息。我创建了一个名为collectionViewOffset的变量,并给它一个初始值0.0。
然后使用:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // getting the scroll offset
    collectionViewOffset = scrollView.contentOffset.y;
    NSLog(@"offset: %f", collectionViewOffset);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

      PhotoCell *cell = (PhotoCell *)[collectionView cellForItemAtIndexPath:indexPath];
      //collectionView.allowsSelection = NO;
      //collectionView.scrollEnabled = NO;

     [cell.superview bringSubviewToFront:cell];
     [UIView animateWithDuration:2.0 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:.2 options:UIViewAnimationOptionCurveLinear animations:^{
     cell.center = CGPointMake(self.view.vWidth/2, self.bannerView.vBottomEdge + collectionViewOffset + 40);
     } completion:^(BOOL finished) {

     }];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接