封装一个NoData的View方便在网络请求失败时调用
1新建一个继承UIView的类:XSNoDataView
头文件里
#import <UIKit/UIKit.h>
typedef void (^ButtonBlock) (id sender);
@interface XSNoDataView : UIView
- (void)addButtonAction:(ButtonBlock)block;
@end
m文件里
#import "XSNoDataView.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@interface XSNoDataView ()
@property (nonatomic, strong, nullable) ButtonBlock block;
@property (nonatomic, strong, nullable) UIButton *button;
@end
@implementation XSNoDataView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(ScreenWidth*3/8, adoptValue(240), ScreenWidth / 4, ScreenWidth / 4);
[self addSubview:imageView];
NSMutableArray *imgArray = [NSMutableArray array];
for (int i=0; i<3; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"netError%d.png",i]];
[imgArray addObject:image];
}
imageView.animationImages = imgArray;
imageView.animationDuration = 6*0.15;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(0, imageView.frame.origin.y+20+ScreenWidth/4, ScreenWidth, 30);
label.textColor = [UIColor lightGrayColor];
label.text = @"亲,您的手机网络不太顺畅喔~";
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 0;
[self addSubview:label];
UIButton *refreshBTN = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshBTN setTitle:@"重新加载" forState:UIControlStateNormal];
[refreshBTN setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
refreshBTN.titleLabel.font = [UIFont systemFontOfSize:14];
refreshBTN.layer.masksToBounds = YES;
refreshBTN.layer.borderWidth = 1;
refreshBTN.layer.cornerRadius = 3;
refreshBTN.layer.borderColor = [UIColor lightGrayColor].CGColor;
refreshBTN.frame = CGRectMake(ScreenWidth/2-50, label.frame.origin.y+60, 100, 30);
[self addSubview:refreshBTN];
[refreshBTN addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];
}
return self;
}
//实现block回调的方法
- (void)addButtonAction:(ButtonBlock)block {
self.block = block;
}
- (void)buttonAction {
if (self.block) {
self.block(self);
}
}
@end
2 在需要的UIViewController里导入这个类
在网络请求失败的方法里调用,block里就是点击刷新(重试)走的方法,如下:
@property (nonatomic, strong, nullable) XSNoDataView *baseView;
if (self.baseView) {
self.baseView.hidden = NO;
}else{
self.baseView = [[XSNoDataView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
self.baseView.backgroundColor = [UIColor whiteColor];
// self.baseView.frame = self.view.frame;
[self.view addSubview:self.baseView];
@weakify(self)//弱引用
[self.baseView addButtonAction:^(id sender) {
@strongify(self)
self.baseView.hidden = YES;
[self loadData];
}];
}