简体   繁体   English

UIImageView使用UICollectionView淡入淡出动画

[英]UIImageView fade animation with UICollectionView

I have a Horizontal UICollectionView with n number of records. 我有一个水平的UICollectionView具有n条记录。 Each cell has an UIImageView which needs to animate the Fadein Fadeout with some TimerInterval. 每个单元都有一个UIImageView ,该UIImageView需要使用一些TimerInterval为Fadein Fadeout设置动画。 So, i have planned to implement that reload my UICollectionView using NSTimer My code snippet, 因此,我计划使用NSTimer实现重新加载UICollectionView代码段,

....
....
if (collectionTimer) {
    [collectionTimer invalidate];
    collectionTimer = nil;
}
collectionTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(reloadCollection) userInfo:nil repeats:YES];
....
....

reloadCollection Method reloadCollection方法

-(void)reloadCollection{
    [_collectionView reloadData];
}

cellForItemAtIndexPath Method cellForItemAtIndexPath方法

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ....
    ....
    /* Basic Animation working fine*/
    [UIView animateKeyframesWithDuration:1.0 delay:1.0 options:UIViewKeyframeAnimationOptionAutoreverse animations:^{
        cell.articleImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", [images objectAtIndex:i]]];
    } completion:nil];

    /* Not working */
    /* [UIView transitionWithView:cell.articleImage duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        cell.articleImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", [images objectAtIndex:i]]];
    } completion:nil]; */
    ....
    ....
}

The basic animation works fine. 基本动画效果很好。 But, whenever i am trying to implement UIViewAnimationOptionTransitionCrossDissolve with transitionWithView method. 但是,每当我尝试使用transitionWithView方法实现UIViewAnimationOptionTransitionCrossDissolve时。 It will showing the animation, but immediately one more image loaded because of reloadData calling. 它将显示动画,但是由于调用了reloadData ,因此会立即加载另外一幅图像。 How to fix this? 如何解决这个问题? How to animate only the images in UICollectionView 如何仅对UICollectionView的图像设置动画

Any Idea appreciated! 任何想法表示赞赏!

Well i guess that in your method cellForItemAtIndexPath you are not configuring animation right. 好吧,我想在您的方法cellForItemAtIndexPath您没有配置动画。

please use this. 请使用这个。

[UIView animateKeyframesWithDuration:1.0 delay:0.1 options:UIViewKeyframeAnimationOptionAutoreverse animations:^{
    cell.articleImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", [images objectAtIndex:i]]];
} completion:nil];

You were not assigning the delay properly to let animation work. 您没有正确分配延迟以使动画正常工作。

Hope it works for you now. 希望它现在对您有用。 Thanks 谢谢

My animation work like this: 我的动画工作如下:

cell.articleImage.alpha = 0.0;
[UIView animateWithDuration:0.3f animations:^{
     [cell.articleImage setAlpha:1.0];
}];

Try to add the animation for visible cells without reloading the collectionView: 尝试为可见单元格添加动画,而无需重新加载collectionView:

NSArray* visibleCells = [collectionView visibleCells];
[visibleCells enumerateObjectsUsingBlock:^(UICollectionViewCell* cell, NSUInteger idx, BOOL *stop) {
    //TODO: add animation;
}];

Update 更新
To animate new cells, you should start animation after adding cell to window. 要为新单元格设置动画,应在将单元格添加到窗口后开始动画。 In cell you can override method didMoveToWindow: 在单元格中,您可以覆盖didMoveToWindow方法:

- (void)didMoveToWindow
{
   [super didMoveToWindow];
   if (self.window && self.shouldStartAnimation)
   {
     [self startAnimation];
   }
}

And in ViewController these methods: 在ViewController中,这些方法是:

    - (void)animateVisibleCells
    {
        NSArray* visibleCells = [self.collectionView visibleCells];
        [visibleCells enumerateObjectsUsingBlock:^(BTCollectionViewCell* cell, NSUInteger idx, BOOL *stop) {
            [cell startAnimation];
        }];
    }


    #define ANIMATION_DURATION (0.3)
    - (void)animateCells
    {
        self.shouldAnimationStart = YES;

        [self animateVisibleCells];

        [NSTimer scheduledTimerWithTimeInterval:ANIMATION_DURATION target:self selector:@selector(endAnimateCells) userInfo:nil repeats:NO];
    }


    - (void)endAnimateCells
    {
        self.shouldAnimationStart = NO;
    }


    - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
    {
        ((BTCollectionViewCell*)cell).shouldStartAnimation = self.shouldAnimationStart;
    }

But problem with synchronization of the animation continues to be (new animations will starts from 0 time). 但是动画同步的问题仍然存在(新动画将从0次开始)。 If you know method to start animation from concrete time, you can use it method in willDisplayCell 如果您知道从具体时间开始动画的方法,则可以在willDisplayCell中使用它的方法

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

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