ZFSpeedLoadingView.m 3.0 KB

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