简体   繁体   English

弹出 collectionview 单元格并缩放 - uicollectionviewcell 动画

[英]Pop collectionview cell out and zoom - uicollectionviewcell animation

Im trying to create a cool animation with my collection view selection.我试图用我的收藏视图选择创建一个很酷的动画。 Basically I have a collection view of photo cells displayed.基本上我有一个显示的照片单元的集合视图。 What I want to happen is have the cell pop out of its location, scale and move to the center of the screen where the user will be prompted to confirm the selection.我想要发生的是让单元格弹出它的位置,缩放并移动到屏幕的中心,在那里用户将被提示确认选择。

Here is my code so far, but currently the animation takes the cells original frame location, so if I scroll it doesnt take into account that the frame position is no longer the same.到目前为止,这是我的代码,但目前动画采用单元格的原始帧位置,所以如果我滚动它不会考虑到帧位置不再相同。

- (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;
}];
}

I solved it myself:我自己解决了:

For those who are experiencing the same problem, we have to take into account how much the collection view has scrolled (offset) and use that information.对于那些遇到同样问题的人,我们必须考虑集合视图滚动(偏移)的程度并使用该信息。 I created a variable called collectionViewOffset and gave it an initial value of 0.0我创建了一个名为 collectionViewOffset 的变量并将其初始值为 0.0

then used:然后使用:

- (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) {

     }];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM