UIView+GHBlank.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // UIView+GHBlank.m
  3. // GameHelper
  4. //
  5. // Created by 青秀斌 on 16/12/29.
  6. // Copyright © 2016年 kylincc. All rights reserved.
  7. //
  8. #import "UIView+GHBlank.h"
  9. #import "GHBlankView.h"
  10. #import <objc/runtime.h>
  11. @implementation UIView (GHBlank)
  12. NS_ASSUME_NONNULL_BEGIN
  13. const char *__blankView__ = "__blankView__";
  14. const char *__blankAction__ = "__blankAction__";
  15. - (void)showBlankWithImage:(nullable NSString *)image
  16. title:(nullable NSString *)title
  17. message:(nullable NSString *)message
  18. action:(nullable void (^)(void))action offsetY:(CGFloat)offsetY{
  19. GHBlankView *blankView = objc_getAssociatedObject(self, __blankView__);
  20. if (blankView == nil) {
  21. blankView = [[GHBlankView alloc] init];
  22. blankView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  23. [self addSubview:blankView];
  24. objc_setAssociatedObject(self, __blankView__, blankView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  25. }
  26. blankView.userInteractionEnabled = NO;
  27. CGRect frame = self.bounds;
  28. //修正TableView上对header和footer的遮挡
  29. if ([self isKindOfClass:[UITableView class]]) {
  30. UITableView *tableView = (UITableView *)self;
  31. frame.origin.y += tableView.tableHeaderView.height;
  32. frame.size.height -= tableView.tableHeaderView.height + tableView.tableFooterView.height;
  33. }
  34. blankView.frame = frame;
  35. blankView.image(image).title(title).message(message).offsetY(offsetY);
  36. if (action) {
  37. objc_setAssociatedObject(self, __blankAction__, action, OBJC_ASSOCIATION_COPY);
  38. UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__blankAction:)];
  39. [blankView addGestureRecognizer:tapGesture];
  40. }
  41. }
  42. - (void)showBlankWithImage:(nullable NSString *)image
  43. title:(nullable NSString *)title
  44. message:(nullable NSString *)message
  45. action:(nullable void (^)(void))action {
  46. [self showBlankWithImage:image title:title message:message action:action offsetY:0];
  47. }
  48. - (void)showBlankWithType:(BlankType)type
  49. title:(nullable NSString *)title
  50. message:(nullable NSString *)message
  51. action:(nullable void (^)(void))action
  52. offsetY:(CGFloat)offsetY{
  53. NSString *image = nil;
  54. switch (type) {
  55. case BlankType_NoData: {
  56. image = @"no_data";
  57. }break;
  58. case BlankType_NoNetWork: {
  59. image = @"no_network";
  60. }break;
  61. case BlankType_LoadFailure: {
  62. image = @"failed_to_load";
  63. }break;
  64. }
  65. [self showBlankWithImage:image title:title message:message action:action offsetY:offsetY];
  66. }
  67. - (void)showBlankWithType:(BlankType)type
  68. title:(nullable NSString *)title
  69. message:(nullable NSString *)message
  70. action:(nullable void (^)(void))action {
  71. [self showBlankWithType:type title:title message:message action:action offsetY:0];
  72. }
  73. - (void)dismissBlank {
  74. GHBlankView *blankView = objc_getAssociatedObject(self, __blankView__);
  75. [blankView removeFromSuperview];
  76. objc_setAssociatedObject(self, __blankView__, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  77. objc_setAssociatedObject(self, __blankAction__, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  78. }
  79. /**********************************************************************/
  80. #pragma mark -
  81. /**********************************************************************/
  82. //暂无数据
  83. - (void)showBlankNoData:(nullable void (^)(void))action offsetY:(CGFloat)offsetY {
  84. __weak __typeof(self) weakSelf = self;
  85. [self showBlankWithType:BlankType_NoData title:action?@"暂无数据,点击刷新":@"暂无数据" message:nil action:^{
  86. if (action) {
  87. [weakSelf dismissBlank];
  88. action();
  89. }
  90. } offsetY:offsetY];
  91. }
  92. - (void)showBlankNoData:(nullable void (^)(void))action {
  93. [self showBlankNoData:action offsetY:0];
  94. }
  95. //网络断开
  96. - (void)showBlankNoNetWork:(nullable void (^)(void))action offsetY:(CGFloat)offsetY {
  97. __weak __typeof(self) weakSelf = self;
  98. [self showBlankWithType:BlankType_NoNetWork title:@"网络已断开,请连接后重试" message:nil action:^{
  99. if (action) {
  100. [weakSelf dismissBlank];
  101. action();
  102. }
  103. } offsetY:offsetY];
  104. }
  105. - (void)showBlankNoNetWork:(nullable void (^)(void))action {
  106. [self showBlankNoNetWork:action offsetY:0];
  107. }
  108. //加载失败
  109. - (void)showBlankLoadFailure:(nullable void (^)(void))action offsetY:(CGFloat)offsetY {
  110. __weak __typeof(self) weakSelf = self;
  111. [self showBlankWithType:BlankType_LoadFailure title:action?@"加载失败,点击刷新":@"加载失败" message:nil action:^{
  112. if (action) {
  113. [weakSelf dismissBlank];
  114. action();
  115. }
  116. } offsetY:offsetY];
  117. }
  118. - (void)showBlankLoadFailure:(nullable void (^)(void))action {
  119. [self showBlankLoadFailure:action offsetY:0];
  120. }
  121. /**********************************************************************/
  122. #pragma mark - Action
  123. /**********************************************************************/
  124. - (void)__blankAction:(id)sender {
  125. void (^action)(void) = objc_getAssociatedObject(self, __blankAction__);
  126. if (action) {
  127. action();
  128. }
  129. }
  130. @end
  131. NS_ASSUME_NONNULL_END