iOS 网络错误view封装

来源:互联网 发布:电子书包的软件 编辑:程序博客网 时间:2024/04/30 04:35

封装一个网络请求失败的view ,有一个重新加载按钮,点击重新走网络请求

新建一个继承于UIView的类

.h文件

#import <UIKit/UIKit.h>typedef void (^ButtonBlock) (id sender);@interface XSNoDataView : UIView- (void)addButtonAction:(ButtonBlock)block;@end

.m文件

#import "XSNoDataView.h"@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
在VC里导入这个类头文件,初始化一个变量self.baseView ,调这个方法,block里就是重新加载的点击事件

            [self.baseView addButtonAction:^(id sender) {                @strongify(self)                self.baseView.hidden = YES;                [self loadData];            }];


0 0
原创粉丝点击