UIView+SPSBDefault.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // UIView+SPSBDefault.m
  3. // 我的社保
  4. //
  5. // Created by shanp on 2021/4/21.
  6. //
  7. #import "UIView+SPSBDefault.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. #import <objc/runtime.h>
  10. @implementation UIView (SPSBDefault)
  11. - (UIView *)defultViewWithType:(SPSBNetworkDataDefault)type image:(nullable UIImage *)image action:(void(^_Nullable)(void))action {
  12. if (!image) {
  13. switch (type) {
  14. case SPSBNetworkDataDefaultNoData:
  15. image = UIImage.new;
  16. break;
  17. case SPSBNetworkDataDefaultMissData:
  18. image = jxh_getImage(loading_failure);
  19. break;
  20. case SPSBNetworkDataDefaultMissNetwork:
  21. image = jxh_getImage(network_error);
  22. default:
  23. break;
  24. }
  25. }
  26. UIView *defaultView = UIView.new;
  27. UIImageView *icon = [[UIImageView alloc] initWithImage:image];
  28. [defaultView addSubview:icon];
  29. [icon makeConstraints:^(JXHConstraintMaker *make) {
  30. make.center.equalTo(0);
  31. }];
  32. if (action) {
  33. self.spsb_action = action;
  34. [defaultView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]];
  35. }
  36. return defaultView;
  37. }
  38. - (void)tapAction {
  39. if (self.spsb_action) {
  40. self.spsb_action();
  41. }
  42. }
  43. - (void)setSpsb_action:(void (^)(void))spsb_action {
  44. objc_setAssociatedObject(self, _cmd, spsb_action, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  45. }
  46. - (void (^)(void))spsb_action {
  47. return objc_getAssociatedObject(self, @selector(setSpsb_action:));
  48. }
  49. @end