ExceptionDefaultView.m
5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// ExceptionDefaultView.m
// DreamSleep
//
// Created by peter on 2022/4/25.
//
#import "ExceptionDefaultView.h"
@interface ExceptionDefaultView ()
@property (nonatomic, strong) UIImageView *exceptionIV;
@property (nonatomic, strong) UILabel *exceptLab;
@property (nonatomic, strong) UIButton *dealBtn;
@property (nonatomic, copy) DealBlock dealBlock;
@property (nonatomic, assign) ExceptionType type;
@end
@implementation ExceptionDefaultView
- (instancetype)initWithType:(ExceptionType)type block:(DealBlock)block superView:(UIView *)superView {
if (self = [super initWithFrame:CGRectZero]) {
_dealBlock = block;
_type = type;
self.hidden = YES;
[superView addSubview:self];
[self addSubview:self.exceptLab];
[self addSubview:self.dealBtn];
if (type == ExceptionTypeSmall) {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.right.equalTo(superView);
}];
[self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.left.equalTo(self).offset(5);
make.right.equalTo(self).offset(-5);
make.centerY.equalTo(self).offset(-30);
}];
[self.dealBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.size.mas_equalTo(CGSizeMake(155, 40));
make.centerY.equalTo(self).offset(30);
}];
} else {
[self addSubview:self.exceptionIV];
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(superView);
make.center.equalTo(superView);
}];
[self.exceptionIV mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self);
}];
[self.exceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.left.equalTo(self).offset(5);
make.right.equalTo(self).offset(-5);
make.top.equalTo(self.exceptionIV.mas_bottom).offset(42);
}];
[self.dealBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.size.mas_equalTo(CGSizeMake(155, 40));
make.top.equalTo(self.exceptLab.mas_bottom).offset(42);
make.bottom.equalTo(self).offset(-30);
}];
}
}
return self;
}
- (void)updateExceptionViewWithType:(ExceptionType)type {
self.exceptionIV.image = [UIImage imageNamed:[self getExceptionImgWithType:type]];
self.exceptLab.text = [self getExceptionInfoWithType:type];
[self.dealBtn setTitle:[self getDealBtnTitleWithType:self.type] forState:UIControlStateNormal];
}
- (void)showServerErrInfo:(NSString *)errInfo {
self.exceptLab.text = (errInfo && [errInfo isKindOfClass:[NSString class]]) ? errInfo : @"服务器异常";
}
#pragma mark - Actions
- (void)dealAction {
if (self.dealBlock) { self.dealBlock(); }
}
#pragma mark - lazy
- (UIImageView *)exceptionIV {
if (!_exceptionIV) {
_exceptionIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self getExceptionImgWithType:self.type]]];
}
return _exceptionIV;
}
- (UILabel *)exceptLab {
if (!_exceptLab) {
_exceptLab = [UILabel labWithText:[self getExceptionInfoWithType:self.type] font:SysFont(14) fit:YES];
_exceptLab.textAlignment = NSTextAlignmentCenter;
_exceptLab.dk_textColorPicker = DKColorPickerWithKey(SubTEXT);
}
return _exceptLab;
}
- (UIButton *)dealBtn {
if (!_dealBtn) {
_dealBtn = [UIButton btnWithTitle:[self getDealBtnTitleWithType:self.type] titleColor:DSWhite font:BoldFont(16) bgColor:BrandColor];
[_dealBtn addTarget:self action:@selector(dealAction) forControlEvents:UIControlEventTouchUpInside];
[_dealBtn cornerRadius:20];
}
return _dealBtn;
}
#pragma mark - private
- (NSString *)getExceptionImgWithType:(ExceptionType)type {
NSString *imgName = @"netDefault";
if (type == ExceptionTypeNet) {
imgName = @"netDefault";
} else if (type == ExceptionTypeSmall) {
imgName = @"netDefault";
} else if (type == ExceptionTypeNoData) {
imgName = @"noData";
}
return imgName;
}
- (NSString *)getExceptionInfoWithType:(ExceptionType)type {
NSString *info = @"";
if (type == ExceptionTypeNet) {
info = @"当前网络环境较差,点击刷新重新加载~";
} else if (type == ExceptionTypeSmall) {
info = @"当前网络环境较差,点击刷新重新加载~";
} else if (type == ExceptionTypeNoData) {
info = @"空空如也哦~";
}
return info;
}
- (NSString *)getDealBtnTitleWithType:(ExceptionType)type {
NSString *title = @"";
if (type == ExceptionTypeNet) {
title = @"刷新";
} else if (type == ExceptionTypeSmall) {
title = @"刷新";
} else if (type == ExceptionTypeNoData) {
title = @"重新获取";
}
return title;
}
@end