废话少说,使用实例如下:
[self.errorView reloadDataWithErrorType:ErrorTypeNetwork];
代码如下:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger,ErrorType) {
ErrorTypeNetwork = 1,//网络异常
ErrorTypeServer = 2,//服务器异常
ErrorTypeNoData = 3,//数据为空
ErrorTypeNone = 4,//无错误
};
@protocol TKErrorViewDelegate <NSObject>
@optional
/// 重试按钮被点击
- (void)retryButtonIsClicked;
/// 当前页面被点击
- (void)currentPageIsClicked;
@end
@protocol TKErrorViewConfigDelegate <NSObject>
@optional
- (UIImage *)imageWithErrorType:(ErrorType)errorType;
- (NSAttributedString *)attributedTextWithErrorType:(ErrorType)errorType;
- (NSAttributedString *)attributedButtonTitleWithErrorType:(ErrorType)errorType;
- (UIButton *)appearanceButton:(UIButton *)button errorType:(ErrorType)errorType;
- (CGFloat)imageLabelSpace;
- (CGFloat)labelButtonSpace;
@end
@interface TKErrorView : UIView
@property (nonatomic,weak) id<TKErrorViewDelegate> delegate;
@property (nonatomic,weak) id<TKErrorViewConfigDelegate> configDelegate;//进行UI设置
- (void)reloadDataWithErrorType:(ErrorType)errorType;
@end
NS_ASSUME_NONNULL_END
实现文件
@interface TKErrorView ()
@property (nonatomic,assign) ErrorType errorType;
@property (nonatomic,assign) CGFloat imageLabelSpace;//图片和描述之间的间距
@property (nonatomic,assign) CGFloat labelButtonSpace;//按钮和描述之间的间距
@property (nonatomic,strong) UIView *contentView;
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *textLabel;
@property (nonatomic,strong) UIButton *button;
@end
@implementation TKErrorView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.hidden = YES;
}
return self;
}
//MARK:- getter -
- (UIView *)contentView {
if (!_contentView) {
_contentView = [UIView new];
[self addSubview:_contentView];
}
return _contentView;
}
- (UIImageView *)imageView {
if (!_imageView) {
_imageView = [UIImageView new];
[self.contentView addSubview:_imageView];
}
return _imageView;
}
- (UILabel *)textLabel {
if (!_textLabel) {
_textLabel = [UILabel new];
[self.contentView addSubview:_textLabel];
}
return _textLabel;
}
- (UIButton *)button {
if (!_button) {
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:@selector(reloadButtonAction) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_button];
}
return _button;
}
//MARK:- 点击事件
- (void)reloadButtonAction {
if (self.delegate && [self.delegate respondsToSelector:@selector(retryButtonIsClicked)]) {
[self.delegate retryButtonIsClicked];
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
if (self.delegate && [self.delegate respondsToSelector:@selector(currentPageIsClicked)]) {
[self.delegate currentPageIsClicked];
}
}
//MARK:- 刷新ErrorView
- (void)reloadDataWithErrorType:(ErrorType)errorType {
if (self.errorType == ErrorTypeNone) {//如果成功过一次就再也不能出现网络错误页面
return;
}
_errorType = errorType;
self.hidden = (errorType == ErrorTypeNone);
self.imageView.image = [self.configDelegate imageWithErrorType:errorType];
self.textLabel.attributedText = [self.configDelegate attributedTextWithErrorType:errorType];
if (self.configDelegate && [self.configDelegate respondsToSelector:@selector(appearanceButton:errorType:)]) {
self.button = [self.configDelegate appearanceButton:self.button errorType:errorType];
}
if (self.configDelegate && [self.configDelegate respondsToSelector:@selector(attributedButtonTitleWithErrorType:)]) {
[self.button setAttributedTitle:[self.configDelegate attributedButtonTitleWithErrorType:errorType] forState:UIControlStateNormal];
}
if (self.configDelegate && [self.configDelegate respondsToSelector:@selector(imageLabelSpace)]) {
self.imageLabelSpace = [self.configDelegate imageLabelSpace];
}else {
self.imageLabelSpace = 10;
}
if (self.configDelegate && [self.configDelegate respondsToSelector:@selector(labelButtonSpace)]) {
self.labelButtonSpace = [self.configDelegate labelButtonSpace];
}else {
self.labelButtonSpace = 10;
}
[self setNeedsLayout];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake((self.bounds.size.width-self.imageView.image.size.width)/2, 0, self.imageView.image.size.width, self.imageView.image.size.height);
[self.textLabel sizeToFit];
self.textLabel.frame = CGRectMake((self.bounds.size.width-self.textLabel.bounds.size.width)/2, CGRectGetMaxY(self.imageView.frame)+self.labelButtonSpace, self.textLabel.frame.size.width, self.textLabel.frame.size.height);
[self.button sizeToFit];
self.button.frame = CGRectMake((self.bounds.size.width-self.button.bounds.size.width)/2, CGRectGetMaxY(self.textLabel.frame), self.button.bounds.size.width, self.button.bounds.size.height);
self.contentView.frame = CGRectMake(0, 0, self.frame.size.width, CGRectGetMaxY(self.button.frame)+self.labelButtonSpace);
self.contentView.center = self.center;
}
@end
网友评论