UICollectionView:1行水平滚动(从右到左),右侧是新单元格的自定义动画

时间:2013-05-21 19:40:53

标签: ios uicollectionview uicollectionviewlayout

  • 我有一个带图像的1行集合视图
  • 我从右向左滚动图像

  • 我想为来自右边的每个单元格制作动画(ala Google +)

此动画可以基于Alpha或帧位置

如何实现?

1 个答案:

答案 0 :(得分:1)

这可能不是您想要的动画,但这是我在UICollectionViewCell子类中所做的。当框架缩小到合适的尺寸时,这将在单元格中消失。我在setPhoto内调用collectionView: cellForItemAtIndexPath:方法。

@implementation HistoryCollectionViewCell

- (void)setPhoto:(Photo*)photo {

    if(_photo != photo) {
        _photo = photo;
    }

    // Set up the animations here
    self.photoQueue.alpha = 0.0f;
    self.cellContainerView.alpha = 0.0f;
    CGRect currentFrame = self.cellContainerView.frame;
    CGFloat width = currentFrame.size.width + 20;
    CGFloat height = currentFrame.size.height + 20;
    CGFloat originX = currentFrame.origin.x - 10;
    CGFloat originY = currentFrame.origin.y - 10;
    CGRect newFrame = CGRectMake(originX, originY, width, height);
    self.cellContainerView.frame = newFrame;

    dispatch_queue_t photoQueue = dispatch_queue_create("com.jeremyfox.SnapTo.photoQueue", NULL);
    dispatch_async(photoQueue, ^{

        __block UIImage* image = nil;

        if (_photo) {
            image = [Photo getImageAtPath:_photo.thumbnail];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            if (image) {
                self.imageView.image = image;

                // Perform the animations here
                [UIView animateWithDuration:0.3f animations:^{
                    self.cellContainerView.alpha = 1.0f;
                    self.cellContainerView.frame = currentFrame;
                }];
            }
        });
    });
}