你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios通過SDWebImage實現圖片加載時的漸變效果

ios通過SDWebImage實現圖片加載時的漸變效果

編輯:IOS開發綜合

先上效果圖:

這些圖片是在我限制了網速的情況下加載的:

實現效果

思路解析

想到漸變屬性的時候,自然而然的想起CATransition這個類

先看整體的實現代碼:

首先找到UIImageView+WebCache.m這個文件中的- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock這個函數(大約在44行處)

修改成這個樣子

- (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意思是 交叉淡化過渡

這個 type 還有幾個兄弟:

  1. kCATransitionFade  //交叉淡化過渡                    
  2. kCATransitionMoveIn  //移動覆蓋原圖                    
  3. kCATransitionPush  //新視圖將舊視圖推出去                    
  4. kCATransitionReveal  //底部顯出來

因為我們的需求是漸變嘛,所以就使用kCATransitionFade

注意啦

一定要在下載圖片的這個Block結束後,把animation去掉[self.layer removeAnimationForKey:@"transition"];

為什麼呢,如果你不去掉,在cell復用的時候,會出現加載重復的情況呢。/壞笑 不信的話,你別加呀。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved