#import "ZGKMeFooterView.h"
#import "AFNetworking.h"
#import "MJExtension.h"
#import "ZGKMeSquareBtn.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
@implementation ZGKMeFooterView
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
// 1.url & 参数
NSString *url = @"http://api.budejie.com/api/api_open.php";
NSMutableDictionary *paraDict = [NSMutableDictionary dictionary];
paraDict[@"a"] = @"square";
paraDict[@"c"] = @"topic";
// [[AFHTTPSessionManager manager] GET:url parameters:paraDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// /*
// -[__NSDictionaryI writeToFile:options:error:]: unrecognized selector sent to instance 0x600000275ac0'
// */
// // 1.该方法会报错,因为NSDictionary没有该方法
// [responseObject writeToFile:@"/Users/winson/Desktop/MyProject/5天/data.plist" options:0 error:nil];
// ZGKLog(@"请求数据成功 - %@ %@", [responseObject class], responseObject)
// } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// ZGKLog(@"请求数据失败 - %@", error)
// }];
// 2.AFNetWorking在发送https请求的时候会有点问题,但是设置为NO,则可以解决
// [AFHTTPSessionManager manager].securityPolicy.validatesDomainName = NO;
[[AFHTTPSessionManager manager] GET:url parameters:paraDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary* responseObject) {
// 通过打印得知,该数据类型是字典,所以可以将id 改为 NSDictionary, 不然运行时调用方法时,会出错(拿来参考返回值的)
// [responseObject writeToFile:@"/Users/winson/Desktop/MyProject/5天/data.plist" atomically:YES];
// 字典转模型(此处是字典数组转模型数组),分类最好加前缀,例如这里的"mj_"
NSArray *squares = [ZGKMeSquareBtn mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
ZGKLog(@"字典转模型%@", squares);
[self creatSquaresBtn:squares];
// ZGKLog(@"请求数据成功 - %@ %@", [responseObject class], responseObject)
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
ZGKLog(@"请求数据失败 - %@", error)
}];
};
return self;
}
/**** 根据模型数据创建对应的控件 ****/
- (void)creatSquaresBtn: (NSArray *)squares{
// 1.方块个数
NSUInteger count = squares.count;
// 2.方块参数
int maxColsCount = 4;
CGFloat maxWidth = self.zgk_width / 4;
CGFloat maxHeight = maxWidth;
// 3.创建按钮并设置模型数据
for (int index = 0; index < count; index ++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[self addSubview:btn];
// 设置btn的frame
btn.zgk_x = (index % maxColsCount) * maxWidth;
btn.zgk_y = (index / maxColsCount) * maxHeight;
btn.zgk_width = maxWidth;
btn.zgk_height = maxHeight;
btn.backgroundColor = ZGKRandomColor;
// 4.拿到模型数据,并赋值
ZGKMeSquareBtn *square = squares[index];
[btn setTitle:square.name forState:UIControlStateNormal];
// 该方法没有指定btn的状态,所以不能设置成功(btn比较特殊)
// 下面这个方法一般用于普通控件,按钮btn比较特殊,需要指定状态
// [btn.imageView sd_setImageWithURL:[NSURL URLWithString:@"square.icon"] placeholderImage:[UIImage imageNamed:@"mine_icon_random"]];
// 可以通过[SDWebImageManager sharedManager]下载以后再进行赋值(这个可以拿到image存储url)
// 需#import "UIImageView+WebCache.h"
// [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:square.icon] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
// [btn setImage:image forState:UIControlStateNormal];
// }];
// 这个方法是btn特有的,这里直接使btn,而不是btn.image来设置图片,因为按钮在设置图片时才要指定状态
// 需#import "UIButton+WebCache.h"
[btn sd_setImageWithURL:[NSURL URLWithString:square.icon] forState:UIControlStateNormal];
// 5.监听按钮的点击
[btn addTarget:self action:@selector(squareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
/**** 超过父控件部分,点击是无法响应的,要么改变父控件的大小,要么重写hitTest方法 ****/
- (void)squareBtnClick:(UIButton *)button{
ZGKLogFunc
}
@end
总结:
1.按钮设置图片和一般控件设置图片不同,需要指定状态才能设置成功
2.超过父控件部分,点击是无法响应的,要么改变父控件的大小,要么重写hitTest方法
3.2.AFNetWorking在发送https请求的时候会有点问题,但是设置为NO,则可以解决:
[AFHTTPSessionManager manager].securityPolicy.validatesDomainName = NO;
4.注意请求回来的对象的真实类型,id类型的对象在打方法的时候,显示出来的并不一定是正确的,可能在运行的时候就会崩溃,如:
- 下面方法会报错,因为NSDictionary没有该方法
[responseObject writeToFile:@"/Users/winson/Desktop/MyProject/5天/data.plist" options:0 error:nil];
- 正确写法如下:
[[AFHTTPSessionManager manager] GET:url parameters:paraDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary* responseObject) {
// 通过打印得知,该数据类型是字典,所以可以将id 改为 NSDictionary, 不然运行时调用方法时,会出错(拿来参考返回值的)
// [responseObject writeToFile:@"/Users/winson/Desktop/MyProject/5天/data.plist" atomically:YES];
// 字典转模型(此处是字典数组转模型数组),分类最好加前缀,例如这里的"mj_"
NSArray *squares = [ZGKMeSquareBtn mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
ZGKLog(@"字典转模型%@", squares);
[self creatSquaresBtn:squares];
// ZGKLog(@"请求数据成功 - %@ %@", [responseObject class], responseObject)
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
ZGKLog(@"请求数据失败 - %@", error)
}];
};