源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

ios通过SDWebImage实现图片加载时的渐变效果

  • 时间:2020-02-12 07:45 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:ios通过SDWebImage实现图片加载时的渐变效果
[b]先上效果图:[/b] 这些图片是在我限制了网速的情况下加载的: [img]http://files.jb51.net/file_images/article/201704/2017413151633396.gif?2017313151653[/img] [b]实现效果[/b] 思路解析 想到渐变属性的时候,自然而然的想起[code]CATransition[/code]这个类 [b]先看整体的实现代码:[/b] 首先找到UIImageView+WebCache.m这个文件中的- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock这个函数(大约在44行处) [b]修改成这个样子[/b]
- (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"];
不需要过多解释[code]kCATransitionFade[/code]意思是 交叉淡化过渡 这个 type 还有几个兄弟: [list=1] [*]kCATransitionFade  //交叉淡化过渡                    [/*] [*]kCATransitionMoveIn  //移动覆盖原图                    [/*] [*]kCATransitionPush  //新视图将旧视图推出去                    [/*] [*]kCATransitionReveal  //底部显出来 [/*] [/list] 因为我们的需求是渐变嘛,所以就使用[code]kCATransitionFade[/code] 注意啦 一定要在下载图片的这个[code]Block[/code]结束后,把[code]animation[/code]去掉[code][self.layer removeAnimationForKey:@"transition"]; [/code]。 为什么呢,如果你不去掉,在cell复用的时候,会出现加载重复的情况呢。/坏笑 不信的话,你别加呀。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部