ZYLEmptyDataView.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // ZYLEmptyDataView.m
  3. // mask
  4. //
  5. // Created by 翟玉磊 on 2018/12/9.
  6. // Copyright © 2018 翟玉磊. All rights reserved.
  7. //
  8. #import "ZYLEmptyDataView.h"
  9. const CGFloat DefaultOffsetTop = 250;
  10. @interface ZYLEmptyDataView ()
  11. /// imageView <图片>
  12. @property (nonatomic , readwrite , weak) UIImageView *imageView;
  13. /// tipsLabel <信息提示>
  14. @property (nonatomic , readwrite , weak) UILabel *tipsLabel;
  15. /// 重新加载
  16. @property (nonatomic , readwrite , weak) UIButton *reloadButton;
  17. /** 重新加载block */
  18. @property (nonatomic , readwrite , copy) void(^reloadBlock)(void);
  19. @end
  20. @implementation ZYLEmptyDataView
  21. #pragma mark - Public Method
  22. - (void)configEmptyViewWithType:(ZYLEmptyDataViewType)type emptyInfo:(NSString *)emptyInfo errorInfo:(NSString *)errorInfo offsetTop:(CGFloat)offsetTop hasData:(BOOL)hasData hasError:(BOOL)hasError reloadBlock:(void(^)(void))reloadBlock{
  23. if (hasData) { /// 有数据,则不需要显示占位图
  24. self.hidden = YES;
  25. [self removeFromSuperview];
  26. return;
  27. }
  28. [self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
  29. make.centerY.equalTo(self.mas_top).with.offset(offsetTop);
  30. }];
  31. self.reloadButton.hidden = ObjectIsNil(reloadBlock);
  32. self.reloadBlock = reloadBlock;
  33. /// 没有数据的情况 1. 确实没有数据 2. 请求出错导致无数据
  34. self.hidden = NO;
  35. UIImage *image = nil;
  36. if (hasError) { /// 请求出错 1. 网络问题 2. 服务器问题
  37. if (![ZYLAPPContext sharedInstance].isReachable) { /// 无网络
  38. errorInfo = StringIsNotEmpty(errorInfo)?errorInfo:@"呀!网络正在开小差~";
  39. image = [UIImage imageNamed:@"icon_nodata_default"];
  40. }else{ /// 服务器出错
  41. errorInfo = StringIsNotEmpty(errorInfo)?errorInfo:@"呜呜!服务器崩溃了~";
  42. image = [UIImage imageNamed:@"icon_nodata_default"];
  43. }
  44. /// 赋值
  45. self.imageView.image = image;
  46. self.tipsLabel.text = errorInfo;
  47. return;
  48. }
  49. /// 无数据
  50. switch (type) {
  51. case ZYLEmptyDataViewTypeDefault: /// 默认情况
  52. {
  53. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"暂无数据");
  54. image = ImageNamed(@"icon_nodata_default");
  55. }
  56. break;
  57. case ZYLEmptyDataViewTypeNoData: /// 电脑图片空界面
  58. {
  59. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"暂无数据");
  60. image = ImageNamed(@"icon_nodata_home");
  61. }
  62. break;
  63. case ZYLEmptyDataViewTypeSearchPersonal:
  64. {
  65. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"没有找到符合条件的人");
  66. image = [UIImage imageNamed:@"icon_nodata_default"];
  67. }
  68. break;
  69. case ZYLEmptyDataViewTypeNoLogin:
  70. {
  71. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"登陆后才能查看信息");
  72. // image = [UIImage imageNamed:@"icon_noData_default"];
  73. [self.reloadButton setTitle:kLocalizedString(@"去登陆") forState:UIControlStateNormal];
  74. }
  75. break;
  76. case ZYLEmptyDataViewTypeAnchorApply:
  77. {
  78. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"为了你的账户安全\n 请通过实名认证绑定账户");
  79. image = [UIImage imageNamed:@"icon_nodata_apply"];
  80. }
  81. break;
  82. case ZYLEmptyDataViewTypeNoAttention:
  83. {
  84. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"暂时没有关注");
  85. image = [UIImage imageNamed:@"icon_nodata_attention"];
  86. }
  87. break;
  88. case ZYLEmptyDataViewTypeNoCoin:
  89. {
  90. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:[NSString stringWithFormat:@"%@%@", kLocalizedString(@"暂时没有"), App_CoinName(Wallet_Type_Star_Diamond)];
  91. image = [UIImage imageNamed:@"icon_nodata_coin"];
  92. }
  93. break;
  94. case ZYLEmptyDataViewTypeNoRecharge:
  95. {
  96. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"暂时没有充值");
  97. image = [UIImage imageNamed:@"icon_nodata_recharge"];
  98. }
  99. break;
  100. case ZYLEmptyDataViewTypeNoRecord:
  101. {
  102. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"暂时没有记录");
  103. image = [UIImage imageNamed:@"icon_nodata_record"];
  104. }
  105. break;
  106. case ZYLEmptyDataViewTypeNoSubscribe:
  107. {
  108. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"暂时没有订阅");
  109. image = [UIImage imageNamed:@"icon_nodata_subscribe"];
  110. }
  111. break;
  112. case ZYLEmptyDataViewTypeNoVisitor:
  113. {
  114. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"暂时没有访客");
  115. image = [UIImage imageNamed:@"icon_nodata_visitor"];
  116. }
  117. break;
  118. case ZYLEmptyDataViewTypeNoLiveCategory:
  119. {
  120. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"请查看网络状态");
  121. image = [UIImage imageNamed:@"icon_nodata_default"];
  122. [self.reloadButton setTitle:kLocalizedString(@"重新连接") forState:UIControlStateNormal];
  123. }
  124. break;
  125. case ZYLEmptyDataViewTypeNoSkillCertification:
  126. {
  127. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:kLocalizedString(@"您还没有认证技能,请前去认证");
  128. image = [UIImage imageNamed:@"icon_nodata_default"];
  129. [self.reloadButton setTitle:kLocalizedString(@"立即认证") forState:UIControlStateNormal];
  130. }
  131. break;
  132. default:
  133. {
  134. emptyInfo = StringIsNotEmpty(emptyInfo)?emptyInfo:@"~ 敬请期待 ~";
  135. image = [UIImage imageNamed:@"icon_nodata_default"];
  136. }
  137. break;
  138. }
  139. /// 赋值
  140. self.imageView.image = image;
  141. self.tipsLabel.text = emptyInfo;
  142. }
  143. #pragma mark - Private Method
  144. - (instancetype)initWithFrame:(CGRect)frame{
  145. if (self = [super initWithFrame:frame]) {
  146. // 初始化
  147. [self _setup];
  148. // 创建自控制器
  149. [self _setupSubViews];
  150. // 布局子控件
  151. [self _makeSubViewsConstraints];
  152. }
  153. return self;
  154. }
  155. #pragma mark - 事件处理Or辅助方法
  156. - (void)_reloadBtnDidClicked:(UIButton *)sender{
  157. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  158. !self.reloadBlock?:self.reloadBlock();
  159. });
  160. }
  161. #pragma mark - Private Method
  162. - (void)_setup{
  163. /// 设置界面背景颜色
  164. self.backgroundColor = Color_Clear;
  165. }
  166. #pragma mark - 创建自控制器
  167. - (void)_setupSubViews{
  168. /// imageView
  169. UIImageView *imageView = [[UIImageView alloc] init];
  170. self.imageView = imageView;
  171. [self addSubview:imageView];
  172. /// tipsLabel
  173. UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  174. tipsLabel.text = nil;
  175. tipsLabel.numberOfLines = 0;
  176. tipsLabel.font = Font_B(14);
  177. tipsLabel.textColor = Color_TextGray;
  178. tipsLabel.textAlignment = NSTextAlignmentCenter;
  179. self.tipsLabel = tipsLabel;
  180. [self addSubview:tipsLabel];
  181. /// reloadButton
  182. UIButton * reloadButton = [[UIButton alloc] initWithFrame:CGRectZero];
  183. reloadButton.titleLabel.font =[UIFont systemFontOfSize:18.f];
  184. [reloadButton setTitleColor:Color_White forState:UIControlStateNormal];
  185. [reloadButton setTitle:kLocalizedString(@"重新连接") forState:UIControlStateNormal];
  186. reloadButton.adjustsImageWhenHighlighted = YES;
  187. [reloadButton setBackgroundImage:[UIImage commonRedGradientColorImageWithImgSize:CGSizeMake(160.0f, 40.0f)] forState:UIControlStateNormal];
  188. [reloadButton addTarget:self action:@selector(_reloadBtnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
  189. self.reloadButton = reloadButton;
  190. [self addSubview:reloadButton];
  191. }
  192. #pragma mark - 布局子控件
  193. - (void)_makeSubViewsConstraints{
  194. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  195. make.centerX.equalTo(self);
  196. make.width.height.equalTo(@(VWIDTH * 120.0f));
  197. make.centerY.equalTo(self.mas_top).with.offset(0);
  198. }];
  199. [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  200. make.left.right.centerX.equalTo(self);
  201. make.top.equalTo(self.imageView.mas_bottom);
  202. make.height.mas_equalTo(50);
  203. }];
  204. [self.reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
  205. make.centerX.equalTo(self);
  206. make.top.equalTo(self.tipsLabel.mas_bottom).offset(20.0f);
  207. make.size.mas_equalTo(CGSizeMake(144, 40));
  208. }];
  209. [self.reloadButton addViewBorder:Color_Clear redian:4];
  210. }
  211. @end