如何使用带有 UICollectionView 的 SDWebImage 添加溶解动画
How to add a dissolve animation with SDWebImage with a UICollectionView
我想使用 SDWebImage
向 imageView 添加一些动画,我的 collectionView 委托函数在这里
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = (Cell *)[cv dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:URL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
cell.imageView.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
cell.imageView.alpha = 1;
}];
}];
}
在SDWebImage下载图片并设置imageView的图片就可以了。
但是当我调用 [collection reloadData]
方法时,这些 imageViews 又消失了。
我知道原因,但我没有解决办法。
我最近一直在为同样的问题而苦苦挣扎,我想我想出了一个易于使用的解决方案。可以看到.
基本上它是一个 UIImageView 类别,仅当图像是从网络下载或从磁盘缓存中获取时才使用动画设置图像(这花费的时间少得多,但仍然很明显)。如果它来自内存 - 不需要动画。当您重新加载您的集合视图图像显然缓存在内存中,所以不会使用动画。让我知道它是否适合你。
如果您要在加载图像之前寻找动画,试试这个:
cell.imageView.alpha=0;
[UIView animateWithDuration:_duration animations:^{
cell.imageView.alpha = 1;
}];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:URL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
如果图片加载需要时间,那么使用这个:
[shopImg sd_setShowActivityIndicatorView:YES];
[shopImg sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray];
在调用 sd_setImageWithURL 以显示 activity 指标之前。
在第一个缓存之后,animateWithDuration 将非常有效。
这是一个解决方法,正如 NKorotkov 所说,您也可以编写一个类别。
PS。我知道这个问题是 2 年前提出的。这个答案是给 SDWebImage 的新手的。
我想使用 SDWebImage
向 imageView 添加一些动画,我的 collectionView 委托函数在这里
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = (Cell *)[cv dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:URL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
cell.imageView.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
cell.imageView.alpha = 1;
}];
}];
}
在SDWebImage下载图片并设置imageView的图片就可以了。
但是当我调用 [collection reloadData]
方法时,这些 imageViews 又消失了。
我知道原因,但我没有解决办法。
我最近一直在为同样的问题而苦苦挣扎,我想我想出了一个易于使用的解决方案。可以看到
基本上它是一个 UIImageView 类别,仅当图像是从网络下载或从磁盘缓存中获取时才使用动画设置图像(这花费的时间少得多,但仍然很明显)。如果它来自内存 - 不需要动画。当您重新加载您的集合视图图像显然缓存在内存中,所以不会使用动画。让我知道它是否适合你。
如果您要在加载图像之前寻找动画,试试这个:
cell.imageView.alpha=0;
[UIView animateWithDuration:_duration animations:^{
cell.imageView.alpha = 1;
}];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:URL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
如果图片加载需要时间,那么使用这个:
[shopImg sd_setShowActivityIndicatorView:YES];
[shopImg sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray];
在调用 sd_setImageWithURL 以显示 activity 指标之前。 在第一个缓存之后,animateWithDuration 将非常有效。
这是一个解决方法,正如 NKorotkov 所说,您也可以编写一个类别。
PS。我知道这个问题是 2 年前提出的。这个答案是给 SDWebImage 的新手的。