实现图片渐变的方式有两种:
方法一:修改SDWebImage的库,找到 #import "UIView+WebCache.h" 这个文件,在大概136行代码中加入(这种方法比较方便,但是更新库的时候回被移除掉,又要重新添加)
CATransition *animation = [CATransition animation];
animation.duration = 0.50f;
animation.type = kCATransitionFade;
animation.removedOnCompletion = YES;
[wself.layer addAnimation:animation forKey:@"transition"];
[wself setNeedsLayout];
如图:
方法二:在sd_setImageWithURL:placeholderImage:completed:方法,图片加载完成的block中添加CATransition,这种方法最好就是自己封装,因为一个项目用到加载图片的地方还是很多的
[self.imageView sd_setImageWithURL:[NSURL URLWithString:albumModel.albumImg] placeholderImage:placeholderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
if (image) {
CATransition *animation = [CATransition animation];
animation.duration = 2.0;
animation.type = kCATransitionFade;
animation.removedOnCompletion = YES;
[self.imageView.layer addAnimation:animation forKey:@"transition"];
}
}];
CATransition属性type还有几种类型:
- kCATransitionFade //交叉淡化过渡
- kCATransitionMoveIn //移动覆盖原图
- kCATransitionPush //新视图将旧视图推出去
- kCATransitionReveal //底部显出来