UIButton+AFNetworking原始碼學習
思路與UIImageView+AFNetworking原始碼學習一致,只是多了不同的狀態:UIControlState,和不同的圖片設定:setImage和setBackgroundImage。
下面只列出與UIImageView的不同之處:
類別定義如下:
@interface UIButton (AFNetworking)
+ (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader;
+ (AFImageDownloader *)sharedImageDownloader;
//setImage with state
- (void)setImageForState:(UIControlState)state
withURL:(NSURL *)url;
- (void)setImageForState:(UIControlState)state
withURL:(NSURL *)url
placeholderImage:(nullable UIImage *)placeholderImage;
- (void)setImageForState:(UIControlState)state
withURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(nullable UIImage *)placeholderImage
success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
//setBackgroundImage with state
- (void)setBackgroundImageForState:(UIControlState)state
withURL:(NSURL *)url;
- (void)setBackgroundImageForState:(UIControlState)state
withURL:(NSURL *)url
placeholderImage:(nullable UIImage *)placeholderImage;
- (void)setBackgroundImageForState:(UIControlState)state
withURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(nullable UIImage *)placeholderImage
success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
//cancle image download with state
- (void)cancelImageDownloadTaskForState:(UIControlState)state;
//cancle backgroundImage download with state
- (void)cancelBackgroundImageDownloadTaskForState:(UIControlState)state;
@end
receipt的管理:
@interface UIButton (_AFNetworking)
@end
@implementation UIButton (_AFNetworking)
#pragma mark - setImage時對應不同state的receipt:af_imageDownloadReceiptKeyForState
static char AFImageDownloadReceiptNormal;
static char AFImageDownloadReceiptHighlighted;
static char AFImageDownloadReceiptSelected;
static char AFImageDownloadReceiptDisabled;
static const char * af_imageDownloadReceiptKeyForState(UIControlState state) {
switch (state) {
case UIControlStateHighlighted:
return &AFImageDownloadReceiptHighlighted;
case UIControlStateSelected:
return &AFImageDownloadReceiptSelected;
case UIControlStateDisabled:
return &AFImageDownloadReceiptDisabled;
case UIControlStateNormal:
default:
return &AFImageDownloadReceiptNormal;
}
}
- (AFImageDownloadReceipt *)af_imageDownloadReceiptForState:(UIControlState)state {
return (AFImageDownloadReceipt *)objc_getAssociatedObject(self, af_imageDownloadReceiptKeyForState(state));
}
- (void)af_setImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt
forState:(UIControlState)state
{
objc_setAssociatedObject(self, af_imageDownloadReceiptKeyForState(state), imageDownloadReceipt, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark - setBackgroundImage時,對應不同state的receipt:af_backgroundImageDownloadReceiptForState
static char AFBackgroundImageDownloadReceiptNormal;
static char AFBackgroundImageDownloadReceiptHighlighted;
static char AFBackgroundImageDownloadReceiptSelected;
static char AFBackgroundImageDownloadReceiptDisabled;
static const char * af_backgroundImageDownloadReceiptKeyForState(UIControlState state) {
switch (state) {
case UIControlStateHighlighted:
return &AFBackgroundImageDownloadReceiptHighlighted;
case UIControlStateSelected:
return &AFBackgroundImageDownloadReceiptSelected;
case UIControlStateDisabled:
return &AFBackgroundImageDownloadReceiptDisabled;
case UIControlStateNormal:
default:
return &AFBackgroundImageDownloadReceiptNormal;
}
}
- (AFImageDownloadReceipt *)af_backgroundImageDownloadReceiptForState:(UIControlState)state {
return (AFImageDownloadReceipt *)objc_getAssociatedObject(self, af_backgroundImageDownloadReceiptKeyForState(state));
}
- (void)af_setBackgroundImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt
forState:(UIControlState)state
{
objc_setAssociatedObject(self, af_backgroundImageDownloadReceiptKeyForState(state), imageDownloadReceipt, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
相關文章
- 原始碼閱讀:AFNetworking(十二)——UIButton+AFNetworking原始碼UI
- vue原始碼學習Vue原始碼
- MMKV原始碼學習原始碼
- EventBus原始碼學習原始碼
- fishhook原始碼學習Hook原始碼
- 學習HashMap原始碼HashMap原始碼
- koa原始碼學習原始碼
- express原始碼學習Express原始碼
- redis原始碼學習Redis原始碼
- Ember原始碼學習原始碼
- go原始碼學習Go原始碼
- Java容器原始碼學習--ArrayList原始碼分析Java原始碼
- PHP 原始碼加密學習PHP原始碼加密
- Okio 框架原始碼學習框架原始碼
- Vue 原始碼學習(一)Vue原始碼
- Masonry 原始碼學習整理原始碼
- 精讀《原始碼學習》原始碼
- java原始碼學習-SpliteratorJava原始碼
- jQuery原始碼學習之$()jQuery原始碼
- Mybatis 原始碼學習(二)MyBatis原始碼
- java原始碼學習-AbstractSequentialListJava原始碼
- 【原始碼學習】ThreadLocal原始碼thread
- EOS原始碼學習系列原始碼
- ObjectMapper原始碼學習ObjectAPP原始碼
- 來聊聊原始碼學習原始碼
- vue observer 原始碼學習VueServer原始碼
- Dialog原始碼學習原始碼
- 關於原始碼學習原始碼
- VeraCrypt原始碼學習-序原始碼
- 【菜鳥讀原始碼】halo✍原始碼學習 (一)原始碼
- 學習RadonDB原始碼(二)原始碼
- Go Gin原始碼學習(一)Go原始碼
- Retrofit原始碼學習筆記原始碼筆記
- 學習RadonDB原始碼(一)原始碼
- goFrame 原始碼學習之 ServerGoFrame原始碼Server
- jQuery原始碼學習之eventjQuery原始碼
- YYImage原始碼剖析與學習原始碼
- 原始碼學習VUE之Observe原始碼Vue