为ImageView添加渐变效果<无侵入式>

话不多说,开篇立言
为imageView添加渐变动画有很多方法

  • 比如新增分类,重写方法
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder {
     __weak typeof(self) weakSelf = self;
    [self sd_setImageWithURL:url placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        // cacheType == SDImageCacheTypeNone 意味着首次加载
        if (image && cacheType == SDImageCacheTypeNone) {
            CATransition *transition = [CATransition animation];
            transition.type = kCATransitionFade;
            transition.duration = 0.3;
            transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [weakSelf.layer addAnimation:transition forKey:nil];
        }

    }];

}

这个方法只适用于小范围的添加动画效果,如果需要全局统一的添加动画效果则需要采取下面的方式。

  • SDWebImage新版本在UIView+webCache的分类中提供了一个sd_imageTransition的关联对象,SDWebImageTransition是只是对CATransition的进一步封装,详细可以访问SDWebImage访问
/**
 The image transition when image load finished. See `SDWebImageTransition`.
 If you specify nil, do not do transition. Defautls to nil.
 */
@property (nonatomic, strong, nullable) SDWebImageTransition *sd_imageTransition;

这个关联对象在下述方法中被引用

- (void)sd_internalSetImageWithURL:(nullable NSURL *)url
                  placeholderImage:(nullable UIImage *)placeholder
                           options:(SDWebImageOptions)options
                      operationKey:(nullable NSString *)operationKey
                     setImageBlock:(nullable SDSetImageBlock)setImageBlock
                          progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
                         completed:(nullable SDExternalCompletionBlock)completedBlock
                           context:(nullable NSDictionary<NSString *, id> *)context;

而sd_setImage的方法最终都会访问这个方法。
如果我们项目需要为全部的网络图片加上渐变动画的话,只需要hook这个方法即可

+ (void)load {
    
    [self swizzleInstanceMethod:@selector(sd_internalSetImageWithURL:placeholderImage:options:operationKey:setImageBlock:progress:completed:context:)];
}

+ (void)swizzleInstanceMethod:(SEL)sel {
    
    SEL other = NSSelectorFromString([NSString stringWithFormat:@"gp_%@",NSStringFromSelector(sel)]);
    
    Method org = class_getInstanceMethod(self, sel);
    Method method = class_getInstanceMethod(self, other);
    
    if (class_addMethod(self, sel, method_getImplementation(method), method_getTypeEncoding(method))) {
        class_replaceMethod(self, other, method_getImplementation(org), method_getTypeEncoding(org));
    } else {
        method_exchangeImplementations(org, method);
    }
}

-(void)gp_sd_internalSetImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options operationKey:(NSString *)operationKey setImageBlock:(SDSetImageBlock)setImageBlock progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDExternalCompletionBlock)completedBlock context:(NSDictionary<NSString *,id> *)context {
    if (!self.sd_imageTransition) {
        self.sd_imageTransition = [SDWebImageTransition fadeTransition];
        self.animationDuration = 0.3;
    }
    
    [self gp_sd_internalSetImageWithURL:url placeholderImage:placeholder options:options operationKey:operationKey setImageBlock:setImageBlock progress:progressBlock completed:completedBlock context:context];
    
}

在该方法调用前,统一为view赋值sd_imageTransition,即可实现为imageView统一添加渐变效果,而不用更改项目的一行代码。

另外 [SDWebImageTransition fadeTransition] 只是工厂方法的一种,还有其他很多的动画效果可以使用。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,904评论 1 32
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,745评论 4 61
  • 新的一年来了,反而觉得开局特别的不好。 七七八八反正就是一堆不顺心的事儿。好不容易今天开心点儿,房东跟我说,房子快...
    喵喵0425阅读 2,773评论 0 0
  • 今天参加了计算机二级的考试 简单来说通过的几率很小 作为一个大一的学生 其实都很迷茫 看到大家都去考试 心想自己也...
    MrsBai阅读 6,172评论 0 1
  • 爱因斯坦所说:“方程对我而言更重要些,因为政治是为当前,而一个方程却是一种永恒的东西(Equations are ...
    guoery阅读 3,443评论 0 48

友情链接更多精彩内容