亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ios通過SDWebImage實(shí)現(xiàn)圖片加載時(shí)的漸變效果

 更新時(shí)間:2017年04月13日 15:19:56   作者:Lee丶Way  
本篇文章主要介紹了ios通過SDWebImage實(shí)現(xiàn)圖片加載時(shí)的漸變效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

先上效果圖:

這些圖片是在我限制了網(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è)兄弟:

  1. kCATransitionFade  //交叉淡化過渡                    
  2. kCATransitionMoveIn  //移動(dòng)覆蓋原圖                    
  3. kCATransitionPush  //新視圖將舊視圖推出去                    
  4. 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)文章

最新評(píng)論