該文章閱讀的SDWebImage的版本為4.3.3。
該類是負責影像載入完成後在控制元件上的展示動畫。
1.公共型別定義
/**
定義動畫選項
*/
typedef UIViewAnimationOptions SDWebImageAnimationOptions;
複製程式碼
/**
用於動畫開始之前要執行的程式碼塊
*/
typedef void (^SDWebImageTransitionPreparesBlock)(__kindof UIView * _Nonnull view, UIImage * _Nullable image, NSData * _Nullable imageData, SDImageCacheType cacheType, NSURL * _Nullable imageURL);
複製程式碼
/**
用於控制展示動畫的程式碼塊
*/
typedef void (^SDWebImageTransitionAnimationsBlock)(__kindof UIView * _Nonnull view, UIImage * _Nullable image);
複製程式碼
/**
用於動畫結束後要執行的程式碼塊
*/
typedef void (^SDWebImageTransitionCompletionBlock)(BOOL finished);
複製程式碼
2.公共屬性
/**
預設情況下,我們將影像設定為動畫開頭的檢視。可以選擇禁用此功能並提供自定義設定影像處理方法
*/
@property (nonatomic, assign) BOOL avoidAutoSetImage;
複製程式碼
/**
動畫的持續時間,以秒為單位。 預設為0.5秒。
*/
@property (nonatomic, assign) NSTimeInterval duration;
複製程式碼
/**
用於macOS上的計時功能
*/
@property (nonatomic, strong, nullable) CAMediaTimingFunction *timingFunction NS_AVAILABLE_MAC(10_7);
複製程式碼
/**
動畫選項
*/
@property (nonatomic, assign) SDWebImageAnimationOptions animationOptions;
複製程式碼
/**
動畫開始前所要執行的程式碼塊
*/
@property (nonatomic, copy, nullable) SDWebImageTransitionPreparesBlock prepares;
複製程式碼
/**
動畫程式碼塊
*/
@property (nonatomic, copy, nullable) SDWebImageTransitionAnimationsBlock animations;
複製程式碼
/**
動畫結束後所要執行的程式碼塊
*/
@property (nonatomic, copy, nullable) SDWebImageTransitionCompletionBlock completion;
複製程式碼
3.Conveniences分類
這個分類用於快速建立動畫。
3.1. 公共屬性
呼叫不同的類屬性獲取不同的動畫效果
@property (nonatomic, class, nonnull, readonly) SDWebImageTransition *fadeTransition;
@property (nonatomic, class, nonnull, readonly) SDWebImageTransition *flipFromLeftTransition;
@property (nonatomic, class, nonnull, readonly) SDWebImageTransition *flipFromRightTransition;
@property (nonatomic, class, nonnull, readonly) SDWebImageTransition *flipFromTopTransition;
@property (nonatomic, class, nonnull, readonly) SDWebImageTransition *flipFromBottomTransition;
@property (nonatomic, class, nonnull, readonly) SDWebImageTransition *curlUpTransition;
@property (nonatomic, class, nonnull, readonly) SDWebImageTransition *curlDownTransition;
複製程式碼
3.2.公共方法
如果類屬性不可用,就呼叫類方法類獲取不同的動畫效果
+ (nonnull instancetype)fadeTransition;
+ (nonnull instancetype)flipFromLeftTransition;
+ (nonnull instancetype)flipFromRightTransition;
+ (nonnull instancetype)flipFromTopTransition;
+ (nonnull instancetype)flipFromBottomTransition;
+ (nonnull instancetype)curlUpTransition;
+ (nonnull instancetype)curlDownTransition;
複製程式碼
3.3.實現
建立不同的動畫併相容macOS
+ (SDWebImageTransition *)fadeTransition {
SDWebImageTransition *transition = [SDWebImageTransition new];
#if SD_UIKIT
transition.animationOptions = UIViewAnimationOptionTransitionCrossDissolve;
#else
transition.animations = ^(__kindof NSView * _Nonnull view, NSImage * _Nullable image) {
CATransition *trans = [CATransition animation];
trans.type = kCATransitionFade;
[view.layer addAnimation:trans forKey:kCATransition];
};
#endif
return transition;
}
+ (SDWebImageTransition *)flipFromLeftTransition {
SDWebImageTransition *transition = [SDWebImageTransition new];
#if SD_UIKIT
transition.animationOptions = UIViewAnimationOptionTransitionFlipFromLeft;
#else
transition.animations = ^(__kindof NSView * _Nonnull view, NSImage * _Nullable image) {
CATransition *trans = [CATransition animation];
trans.type = kCATransitionPush;
trans.subtype = kCATransitionFromLeft;
[view.layer addAnimation:trans forKey:kCATransition];
};
#endif
return transition;
}
+ (SDWebImageTransition *)flipFromRightTransition {
SDWebImageTransition *transition = [SDWebImageTransition new];
#if SD_UIKIT
transition.animationOptions = UIViewAnimationOptionTransitionFlipFromRight;
#else
transition.animations = ^(__kindof NSView * _Nonnull view, NSImage * _Nullable image) {
CATransition *trans = [CATransition animation];
trans.type = kCATransitionPush;
trans.subtype = kCATransitionFromRight;
[view.layer addAnimation:trans forKey:kCATransition];
};
#endif
return transition;
}
+ (SDWebImageTransition *)flipFromTopTransition {
SDWebImageTransition *transition = [SDWebImageTransition new];
#if SD_UIKIT
transition.animationOptions = UIViewAnimationOptionTransitionFlipFromTop;
#else
transition.animations = ^(__kindof NSView * _Nonnull view, NSImage * _Nullable image) {
CATransition *trans = [CATransition animation];
trans.type = kCATransitionPush;
trans.subtype = kCATransitionFromTop;
[view.layer addAnimation:trans forKey:kCATransition];
};
#endif
return transition;
}
+ (SDWebImageTransition *)flipFromBottomTransition {
SDWebImageTransition *transition = [SDWebImageTransition new];
#if SD_UIKIT
transition.animationOptions = UIViewAnimationOptionTransitionFlipFromBottom;
#else
transition.animations = ^(__kindof NSView * _Nonnull view, NSImage * _Nullable image) {
CATransition *trans = [CATransition animation];
trans.type = kCATransitionPush;
trans.subtype = kCATransitionFromBottom;
[view.layer addAnimation:trans forKey:kCATransition];
};
#endif
return transition;
}
+ (SDWebImageTransition *)curlUpTransition {
SDWebImageTransition *transition = [SDWebImageTransition new];
#if SD_UIKIT
transition.animationOptions = UIViewAnimationOptionTransitionCurlUp;
#else
transition.animations = ^(__kindof NSView * _Nonnull view, NSImage * _Nullable image) {
CATransition *trans = [CATransition animation];
trans.type = kCATransitionReveal;
trans.subtype = kCATransitionFromTop;
[view.layer addAnimation:trans forKey:kCATransition];
};
#endif
return transition;
}
+ (SDWebImageTransition *)curlDownTransition {
SDWebImageTransition *transition = [SDWebImageTransition new];
#if SD_UIKIT
transition.animationOptions = UIViewAnimationOptionTransitionCurlDown;
#else
transition.animations = ^(__kindof NSView * _Nonnull view, NSImage * _Nullable image) {
CATransition *trans = [CATransition animation];
trans.type = kCATransitionReveal;
trans.subtype = kCATransitionFromBottom;
[view.layer addAnimation:trans forKey:kCATransition];
};
#endif
return transition;
}
複製程式碼
4.實現
實現中只做做了在初始化方法中設定預設展示時間為0.5秒
- (instancetype)init {
self = [super init];
if (self) {
self.duration = 0.5;
}
return self;
}
複製程式碼
5.總結
這個類的總結也沒啥好說的,就是幫助建立動畫的類。
值得說的一點是,有一個屬性關鍵字class
,是Xcode 8
中新增的,我對這個關鍵字的理解是:這個關鍵字用於宣告一個“類屬性”,平常我們宣告的都是“物件屬性”,可以類比於“類方法”和“物件方法”的關係進行理解。使用了這個關鍵字的屬性,需要宣告靜態變數儲存,需要手寫getter和setter。在使用時,這個類屬性相當於一個類內的靜態全域性變數,也可以類比成一個單例物件的物件屬性,即這個類屬性在這個類內是共享的。想要了解更多可以看這篇文章:Swift 3.0 令人興奮,但Objective-C也有小改進--Objective-C的類屬性
原始碼閱讀系列:SDWebImage
原始碼閱讀:SDWebImage(二)——SDWebImageCompat
原始碼閱讀:SDWebImage(三)——NSData+ImageContentType
原始碼閱讀:SDWebImage(四)——SDWebImageCoder
原始碼閱讀:SDWebImage(五)——SDWebImageFrame
原始碼閱讀:SDWebImage(六)——SDWebImageCoderHelper
原始碼閱讀:SDWebImage(七)——SDWebImageImageIOCoder
原始碼閱讀:SDWebImage(八)——SDWebImageGIFCoder
原始碼閱讀:SDWebImage(九)——SDWebImageCodersManager
原始碼閱讀:SDWebImage(十)——SDImageCacheConfig
原始碼閱讀:SDWebImage(十一)——SDImageCache
原始碼閱讀:SDWebImage(十二)——SDWebImageDownloaderOperation
原始碼閱讀:SDWebImage(十三)——SDWebImageDownloader
原始碼閱讀:SDWebImage(十四)——SDWebImageManager
原始碼閱讀:SDWebImage(十五)——SDWebImagePrefetcher
原始碼閱讀:SDWebImage(十六)——SDWebImageTransition
原始碼閱讀:SDWebImage(十七)——UIView+WebCacheOperation
原始碼閱讀:SDWebImage(十八)——UIView+WebCache
原始碼閱讀:SDWebImage(十九)——UIImage+ForceDecode/UIImage+GIF/UIImage+MultiFormat
原始碼閱讀:SDWebImage(二十)——UIButton+WebCache
原始碼閱讀:SDWebImage(二十一)——UIImageView+WebCache/UIImageView+HighlightedWebCache