最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)論

虹口区| 莆田市| 灌南县| 化州市| 牟定县| 镇赉县| 南郑县| 和林格尔县| 龙南县| 乌鲁木齐县| 黄大仙区| 明星| 乌拉特后旗| 阜康市| 乡宁县| 社旗县| 镇康县| 贡觉县| 纳雍县| 泸水县| 广宗县| 峨边| 前郭尔| 武城县| 蕉岭县| 喀喇| 南投市| 镇平县| 稷山县| 江永县| 德阳市| 龙井市| 桓仁| 东乡县| 马公市| 赤壁市| 沙湾县| 铅山县| 三都| 漳浦县| 独山县|