CNLiveErrorOrNoNetView.m
6.36 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// CNLiveErrorOrNoNetView.m
// CNLiveMyOrderModule
//
// Created by 殷巧娟 on 2019/11/19.
//
#import "CNLiveErrorOrNoNetView.h"
#import <QMUIKit/QMUIKit.h>
#import <Masonry/Masonry.h>
#import <objc/runtime.h>
#import <CNLiveBaseKit/ColorMacro.h>
#import "CNLiveCommonCategory.h"
static const char retryKey;
@interface CNLiveErrorOrNoNetView ()
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *errorLab;
@property (nonatomic, strong) UILabel *tipLab;;
@property (nonatomic, strong) UIButton *retryBtn;
@end
@implementation CNLiveErrorOrNoNetView
- (void)dealloc
{
NSLog(@"CNLiveErrorOrNoNetView dealloc");
}
- (instancetype)initWithView:(UIView *)view type:(RetryType)type
{
self = [super initWithFrame:view.bounds];
if (self) {
self.backgroundColor = kWhiteColor;
[self addSubview:self.iconView];
[self addSubview:self.errorLab];
[self addSubview:self.tipLab];
[self addSubview:self.retryBtn];
if (type == RetryTypeError) {
self.errorLab.text = @"接口请求错误";
} else {
self.errorLab.text = @"无法连接网络";
}
[self p_addMasonry];
}
return self;
}
+ (CNLiveErrorOrNoNetView *)showErrorByType:(RetryType)type view:(UIView *)view hidenRetry:(BOOL)hidenRetry handle:(RetryHandle)handle {
if (hidenRetry) {
if (type == RetryTypeError) {
[QMUITips showError:@"接口请求失败,请稍后再试!" inView:view hideAfterDelay:1.5];
} else {
[QMUITips showError:@"网络连接失败,请稍后再试!" inView:view hideAfterDelay:1.5];
}
return nil;
}
CNLiveErrorOrNoNetView *tipView = [self tipViewInView:view];
if (!tipView) {
tipView = [[CNLiveErrorOrNoNetView alloc] initWithView:view type:type];
if (handle) {
objc_setAssociatedObject (tipView , &retryKey, handle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
[view addSubview:tipView];
}
return tipView;
}
+ (instancetype)tipViewInView:(UIView *)view {
NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
for (UIView *subview in subviewsEnum) {
if ([subview isKindOfClass:self]) {
return (CNLiveErrorOrNoNetView *)subview;
}
}
return nil;
}
+ (CNLiveErrorOrNoNetView *)showErrorByType:(RetryType)type view:(UIView *)view hidenRetry:(BOOL)hidenRetry handle:(RetryHandle)handle frame:(CGRect)frame {
if (hidenRetry) {
if (type == RetryTypeError) {
[QMUITips showError:@"接口请求失败,请稍后再试!" inView:view hideAfterDelay:1.5];
} else {
[QMUITips showError:@"网络连接失败,请稍后再试!" inView:view hideAfterDelay:1.5];
}
return nil;
}
CNLiveErrorOrNoNetView *tipView = [self tipViewInView:view];
if (!tipView) {
tipView = [[CNLiveErrorOrNoNetView alloc] initWithView:view type:type frame:frame];
if (handle) {
objc_setAssociatedObject (tipView , &retryKey, handle, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
[view addSubview:tipView];
}
return tipView;
}
- (instancetype)initWithView:(UIView *)view type:(RetryType)type frame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.iconView];
[self addSubview:self.errorLab];
[self addSubview:self.tipLab];
[self addSubview:self.retryBtn];
if (type == RetryTypeError) {
self.errorLab.text = @"接口请求错误";
} else {
self.errorLab.text = @"无法连接网络";
}
[self p_addMasonry];
}
return self;
}
#pragma mark -
#pragma mark - Action
- (void)retryBtnAction {
RetryHandle handle = objc_getAssociatedObject(self, &retryKey);
if (handle) {
handle();
}
[self removeFromSuperview];
}
- (void)p_addMasonry {
[self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(80, 80));
make.centerX.mas_equalTo(self.mas_centerX);
make.centerY.mas_equalTo(self.mas_centerY).mas_offset(-79);
}];
[self.errorLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mas_centerX);
make.top.mas_equalTo(self.iconView.mas_bottom).mas_offset(15);
}];
[self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mas_centerX);
make.top.mas_equalTo(self.errorLab.mas_bottom).mas_offset(14);
}];
[self.retryBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.mas_equalTo(self.mas_centerX);
make.top.mas_equalTo(self.tipLab.mas_bottom).mas_offset(16);
make.size.mas_equalTo(CGSizeMake(84.5, 28));
}];
}
#pragma mark -
#pragma mark - Getter
- (UIImageView *)iconView
{
if (!_iconView) {
_iconView = [[UIImageView alloc] init];
_iconView.image = [UIImage cnLive_getImageNameWithBundle:@"wufalianjie" bundle:@"CNLiveMyOrderModule.bundle/CNLiveMyOrderModule" targetClass:[self class]];
}
return _iconView;
}
- (UILabel *)errorLab
{
if (!_errorLab) {
_errorLab = [[UILabel alloc] init];
_errorLab.textColor = KGray102Color;
_errorLab.font = UIFontMake(17);
_errorLab.textAlignment = NSTextAlignmentCenter;
}
return _errorLab;
}
- (UILabel *)tipLab
{
if (!_tipLab) {
_tipLab = [[UILabel alloc] init];
_tipLab.text = @"别紧张,试试看刷新页面~";
_tipLab.textColor = KGray153Color;
_tipLab.font = UIFontMake(13);
_tipLab.textAlignment = NSTextAlignmentCenter;
}
return _tipLab;
}
- (UIButton *)retryBtn
{
if (!_retryBtn) {
_retryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_retryBtn.backgroundColor = UIColorMake(0, 194, 0);
[_retryBtn setTitle:@"重试" forState:UIControlStateNormal];
[_retryBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
_retryBtn.layer.cornerRadius = 6;
_retryBtn.layer.masksToBounds = YES;
[_retryBtn addTarget:self action:@selector(retryBtnAction) forControlEvents:UIControlEventTouchUpInside];
}
return _retryBtn;
}
@end