GKProgressView.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // GKProgressView.m
  3. // GKPhotoBrowser
  4. //
  5. // Created by QuintGao on 2023/3/1.
  6. //
  7. #import "GKProgressView.h"
  8. #import <GKSliderView/GKSliderView.h>
  9. @interface GKProgressView()<GKSliderViewDelegate>
  10. @property (nonatomic, strong) UILabel *currentLabel;
  11. @property (nonatomic, strong) GKSliderView *sliderView;
  12. @property (nonatomic, strong) UILabel *totalLabel;
  13. @property (nonatomic, assign) NSTimeInterval totalTime;
  14. @property (nonatomic, assign) BOOL isSeeking;
  15. @end
  16. @implementation GKProgressView
  17. - (instancetype)init {
  18. if (self = [super init]) {
  19. [self initUI];
  20. }
  21. return self;
  22. }
  23. - (void)layoutSubviews {
  24. [super layoutSubviews];
  25. [self.currentLabel sizeToFit];
  26. [self.totalLabel sizeToFit];
  27. CGFloat x = 0;
  28. CGFloat y = 0;
  29. CGFloat w = 0;
  30. CGFloat h = self.bounds.size.height;
  31. CGFloat totalW = self.bounds.size.width;
  32. CGFloat margin = 5;
  33. w = self.currentLabel.frame.size.width;
  34. self.currentLabel.frame = CGRectMake(x, y, w, h);
  35. w = self.totalLabel.frame.size.width;
  36. x = totalW - w;
  37. self.totalLabel.frame = CGRectMake(x, y, w, h);
  38. x = CGRectGetWidth(self.currentLabel.frame) + margin;
  39. w = totalW - x - margin - CGRectGetWidth(self.totalLabel.frame);
  40. self.sliderView.frame = CGRectMake(x, y, w, h);
  41. }
  42. #pragma mark - Public
  43. - (void)updateCurrentTime:(NSTimeInterval)currentTime totalTime:(NSTimeInterval)totalTime {
  44. if (self.isSeeking) return;
  45. self.totalTime = totalTime;
  46. CGFloat progress = 0;
  47. if (totalTime == 0) {
  48. progress = 0;
  49. }else {
  50. progress = currentTime / totalTime;
  51. }
  52. if (progress <= 0) progress = 0;
  53. if (progress >= 1) progress = 1;
  54. self.sliderView.value = progress;
  55. self.currentLabel.text = [self convertTimeSecond:currentTime];
  56. self.totalLabel.text = [self convertTimeSecond:totalTime];
  57. }
  58. #pragma mark - GKSliderViewDelegate
  59. - (void)sliderView:(GKSliderView *)sliderView touchBegan:(float)value {
  60. [self showLargeSlider];
  61. self.isSeeking = YES;
  62. }
  63. - (void)sliderView:(GKSliderView *)sliderView touchEnded:(float)value {
  64. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showSmallSlider) object:nil];
  65. [self performSelector:@selector(showSmallSlider) withObject:nil afterDelay:3.0f];
  66. if (!self.player) return;
  67. __weak __typeof(self) weakSelf = self;
  68. [self.player gk_seekToTime:self.totalTime * value completionHandler:^(BOOL finished) {
  69. __strong __typeof(weakSelf) self = weakSelf;
  70. self.isSeeking = NO;
  71. }];
  72. }
  73. #pragma mark - Private
  74. - (void)initUI {
  75. [self addSubview:self.currentLabel];
  76. [self addSubview:self.sliderView];
  77. [self addSubview:self.totalLabel];
  78. [self showSmallSlider];
  79. }
  80. - (void)showSmallSlider {
  81. CGRect frame = self.sliderView.sliderBtn.frame;
  82. frame.size = CGSizeMake(6, 6);
  83. self.sliderView.sliderBtn.frame = frame;
  84. self.sliderView.sliderBtn.layer.cornerRadius = 3;
  85. self.currentLabel.hidden = YES;
  86. self.totalLabel.hidden = YES;
  87. }
  88. - (void)showLargeSlider {
  89. CGRect frame = self.sliderView.sliderBtn.frame;
  90. frame.size = CGSizeMake(20, 20);
  91. self.sliderView.sliderBtn.frame = frame;
  92. self.sliderView.sliderBtn.layer.cornerRadius = 10;
  93. self.currentLabel.hidden = NO;
  94. self.totalLabel.hidden = NO;
  95. }
  96. - (NSString *)convertTimeSecond:(NSInteger)timeSecond {
  97. NSString *theLastTime = nil;
  98. long second = timeSecond;
  99. if (timeSecond < 60) {
  100. theLastTime = [NSString stringWithFormat:@"00:%02zd", second];
  101. } else if(timeSecond >= 60 && timeSecond < 3600){
  102. theLastTime = [NSString stringWithFormat:@"%02zd:%02zd", second/60, second%60];
  103. } else if(timeSecond >= 3600){
  104. theLastTime = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", second/3600, second%3600/60, second%60];
  105. }
  106. return theLastTime;
  107. }
  108. #pragma mark - Lazy
  109. - (UILabel *)currentLabel {
  110. if (!_currentLabel) {
  111. _currentLabel = [[UILabel alloc] init];
  112. _currentLabel.font = [UIFont systemFontOfSize:14];
  113. _currentLabel.textColor = UIColor.whiteColor;
  114. _currentLabel.text = @"00:00";
  115. }
  116. return _currentLabel;
  117. }
  118. - (GKSliderView *)sliderView {
  119. if (!_sliderView) {
  120. _sliderView = [[GKSliderView alloc] init];
  121. _sliderView.sliderHeight = 2;
  122. _sliderView.sliderBtn.backgroundColor = UIColor.whiteColor;
  123. _sliderView.sliderBtn.layer.masksToBounds = YES;
  124. _sliderView.delegate = self;
  125. _sliderView.isSliderAllowTapped = NO;
  126. _sliderView.maximumTrackTintColor = UIColor.grayColor;
  127. _sliderView.minimumTrackTintColor = UIColor.whiteColor;
  128. }
  129. return _sliderView;
  130. }
  131. - (UILabel *)totalLabel {
  132. if (!_totalLabel) {
  133. _totalLabel = [[UILabel alloc] init];
  134. _totalLabel.font = [UIFont systemFontOfSize:14];
  135. _totalLabel.textColor = UIColor.whiteColor;
  136. _totalLabel.text = @"00:00";
  137. }
  138. return _totalLabel;
  139. }
  140. @end