UIScrollView+Empty.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. [self bringSubviewToFront:view];
  47. // [UIView animateWithDuration:duration animations:^{
  48. // view.alpha = 1.0f;
  49. // }];
  50. }
  51. - (void)hideEmpty {
  52. UIView *view = [self emptyContentView];
  53. // [UIView animateWithDuration:duration animations:^{
  54. // view.alpha = 0.0f;
  55. // } completion:^(BOOL finished) {
  56. view.hidden = YES;
  57. // }];
  58. [self sendSubviewToBack:view];
  59. }
  60. @end