ios通過SDWebImage實(shí)現(xiàn)圖片加載時(shí)的漸變效果
先上效果圖:
這些圖片是在我限制了網(wǎng)速的情況下加載的:
實(shí)現(xiàn)效果
思路解析
想到漸變屬性的時(shí)候,自然而然的想起CATransition
這個(gè)類
先看整體的實(shí)現(xiàn)代碼:
首先找到UIImageView+WebCache.m這個(gè)文件中的- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock這個(gè)函數(shù)(大約在44行處)
修改成這個(gè)樣子
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock { [self sd_cancelCurrentImageLoad]; objc_setAssociatedObject(self, &imageURLKey, url, OBJC_ASSOCIATION_RETAIN_NONATOMIC); if (!(options & SDWebImageDelayPlaceholder)) { dispatch_main_async_safe(^{ self.image = placeholder; }); } if (url) { // check if activityView is enabled or not if ([self showActivityIndicatorView]) { [self addActivityIndicator]; } __weak __typeof(self)wself = self; id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { [wself removeActivityIndicator]; if (!wself) return; dispatch_main_sync_safe(^{ if (!wself) return; if (image && (options & SDWebImageAvoidAutoSetImage) && completedBlock) { completedBlock(image, error, cacheType, url); return; } else if (image) { CATransition *animation = [CATransition animation]; animation.duration = .85f; animation.type = kCATransitionFade; animation.removedOnCompletion = YES; [wself.layer addAnimation:animation forKey:@"transition"]; wself.image = image; [wself setNeedsLayout]; } else { if ((options & SDWebImageDelayPlaceholder)) { wself.image = placeholder; [wself setNeedsLayout]; } } if (completedBlock && finished) { completedBlock(image, error, cacheType, url); } }); }]; [self.layer removeAnimationForKey:@"transition"]; [self sd_setImageLoadOperation:operation forKey:@"UIImageViewImageLoad"]; } else { dispatch_main_async_safe(^{ [self removeActivityIndicator]; if (completedBlock) { NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}]; completedBlock(nil, error, SDImageCacheTypeNone, url); } }); } }
在大約30行處添加
CATransition *animation = [CATransition animation]; animation.duration = .85f; animation.type = kCATransitionFade; animation.removedOnCompletion = YES; [wself.layer addAnimation:animation forKey:@"transition"];
不需要過多解釋kCATransitionFade
意思是 交叉淡化過渡
這個(gè) type 還有幾個(gè)兄弟:
- kCATransitionFade //交叉淡化過渡
- kCATransitionMoveIn //移動(dòng)覆蓋原圖
- kCATransitionPush //新視圖將舊視圖推出去
- kCATransitionReveal //底部顯出來
因?yàn)槲覀兊男枨笫菨u變嘛,所以就使用kCATransitionFade
注意啦
一定要在下載圖片的這個(gè)Block
結(jié)束后,把animation
去掉[self.layer removeAnimationForKey:@"transition"];
。
為什么呢,如果你不去掉,在cell復(fù)用的時(shí)候,會(huì)出現(xiàn)加載重復(fù)的情況呢。/壞笑 不信的話,你別加呀。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 粒子系統(tǒng) (CAEmitterLayer)實(shí)例詳解
這篇文章主要介紹了IOS 粒子系統(tǒng) (CAEmitterLayer)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09詳解Objective-C編程中對(duì)設(shè)計(jì)模式中適的配器模式的使用
這篇文章主要介紹了Objective-C編程中對(duì)設(shè)計(jì)模式中適的配器模式的使用,適配器模式中的Adapter適配器允許接口不兼容的類在一起工作,需要的朋友可以參考下2016-03-03