UIScrollView+Empty.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // UIScrollView+Empty.m
  3. // UIScrollViewEmpty
  4. //
  5. // Created by Jack on 2018/9/5.
  6. // Copyright © 2018年 United Software Applications. All rights reserved.
  7. //
  8. #import "UIScrollView+Empty.h"
  9. #import "RAEmptyDataView.h"
  10. #import <objc/runtime.h>
  11. static const char *emptyKey = "emptyKey";
  12. static const float duration = 0.25;
  13. @implementation UIScrollView (Empty)
  14. - (void)setEmptyContentView:(RAEmptyDataView *)contentView {
  15. if (contentView) {
  16. UIView *view = objc_getAssociatedObject(self, emptyKey);
  17. if (view) {
  18. [view removeFromSuperview];
  19. }
  20. [self insertSubview:contentView atIndex:0];
  21. [self willChangeValueForKey:@"emptyView"];
  22. objc_setAssociatedObject(self, emptyKey, contentView, OBJC_ASSOCIATION_RETAIN);
  23. [self didChangeValueForKey:@"emptyView"];
  24. }
  25. }
  26. - (RAEmptyDataView *)emptyContentView {
  27. RAEmptyDataView *contentView = objc_getAssociatedObject(self, emptyKey);
  28. if (!contentView) {
  29. contentView = [RAEmptyDataView new];
  30. contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  31. contentView.hidden = YES;
  32. [self setEmptyContentView:contentView];
  33. }
  34. return contentView;
  35. }
  36. - (void)setEmptyView:(UIView *)emptyView {
  37. [self emptyContentView].emptyView = emptyView;
  38. }
  39. - (UIView *)emptyView {
  40. return [self emptyContentView].emptyView;
  41. }
  42. - (void)showEmpty {
  43. UIView *view = [self emptyContentView];
  44. // view.alpha = 0.0f;
  45. view.hidden = NO;
  46. // [UIView animateWithDuration:duration animations:^{
  47. // view.alpha = 1.0f;
  48. // }];
  49. }
  50. - (void)hideEmpty {
  51. UIView *view = [self emptyContentView];
  52. // [UIView animateWithDuration:duration animations:^{
  53. // view.alpha = 0.0f;
  54. // } completion:^(BOOL finished) {
  55. view.hidden = YES;
  56. // }];
  57. }
  58. @end