ZFSpeedLoadingView.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // ZFSpeedLoadingView.m
  3. // Pods-ZFPlayer_Example
  4. //
  5. // Created by 紫枫 on 2018/6/27.
  6. //
  7. #import "ZFSpeedLoadingView.h"
  8. #import "ZFNetworkSpeedMonitor.h"
  9. #import "UIView+ZFFrame.h"
  10. @interface ZFSpeedLoadingView ()
  11. @property (nonatomic, strong) ZFNetworkSpeedMonitor *speedMonitor;
  12. @end
  13. @implementation ZFSpeedLoadingView
  14. - (instancetype)initWithFrame:(CGRect)frame {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. [self initialize];
  18. }
  19. return self;
  20. }
  21. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  22. if (self = [super initWithCoder:aDecoder]) {
  23. [self initialize];
  24. }
  25. return self;
  26. }
  27. - (void)awakeFromNib {
  28. [super awakeFromNib];
  29. [self initialize];
  30. }
  31. - (void)initialize {
  32. self.userInteractionEnabled = NO;
  33. [self addSubview:self.loadingView];
  34. [self addSubview:self.speedTextLabel];
  35. [self.speedMonitor startNetworkSpeedMonitor];
  36. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkSpeedChanged:) name:ZFDownloadNetworkSpeedNotificationKey object:nil];
  37. }
  38. - (void)dealloc {
  39. [self.speedMonitor stopNetworkSpeedMonitor];
  40. [[NSNotificationCenter defaultCenter] removeObserver:self name:ZFDownloadNetworkSpeedNotificationKey object:nil];
  41. }
  42. - (void)layoutSubviews {
  43. [super layoutSubviews];
  44. CGFloat min_x = 0;
  45. CGFloat min_y = 0;
  46. CGFloat min_w = 0;
  47. CGFloat min_h = 0;
  48. CGFloat min_view_w = self.zf_width;
  49. CGFloat min_view_h = self.zf_height;
  50. min_w = 44;
  51. min_h = min_w;
  52. min_x = (min_view_w - min_w) / 2;
  53. min_y = (min_view_h - min_h) / 2 - 10;
  54. self.loadingView.frame = CGRectMake(min_x, min_y, min_w, min_h);
  55. min_x = 0;
  56. min_y = self.loadingView.zf_bottom+5;
  57. min_w = min_view_w;
  58. min_h = 20;
  59. self.speedTextLabel.frame = CGRectMake(min_x, min_y, min_w, min_h);
  60. }
  61. - (void)networkSpeedChanged:(NSNotification *)sender {
  62. NSString *downloadSpped = [sender.userInfo objectForKey:ZFNetworkSpeedNotificationKey];
  63. self.speedTextLabel.text = downloadSpped;
  64. }
  65. - (void)startAnimating {
  66. [self.loadingView startAnimating];
  67. self.hidden = NO;
  68. }
  69. - (void)stopAnimating {
  70. [self.loadingView stopAnimating];
  71. self.hidden = YES;
  72. }
  73. - (UILabel *)speedTextLabel {
  74. if (!_speedTextLabel) {
  75. _speedTextLabel = [UILabel new];
  76. _speedTextLabel.textColor = [UIColor whiteColor];
  77. _speedTextLabel.font = [UIFont systemFontOfSize:12.0];
  78. _speedTextLabel.textAlignment = NSTextAlignmentCenter;
  79. }
  80. return _speedTextLabel;
  81. }
  82. - (ZFNetworkSpeedMonitor *)speedMonitor {
  83. if (!_speedMonitor) {
  84. _speedMonitor = [[ZFNetworkSpeedMonitor alloc] init];
  85. }
  86. return _speedMonitor;
  87. }
  88. - (ZFLoadingView *)loadingView {
  89. if (!_loadingView) {
  90. _loadingView = [[ZFLoadingView alloc] init];
  91. _loadingView.lineWidth = 0.8;
  92. _loadingView.duration = 1;
  93. _loadingView.hidesWhenStopped = YES;
  94. }
  95. return _loadingView;
  96. }
  97. @end