VLCTransportBar.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCTransportBar.h"
  12. #import "VLCBufferingBar.h"
  13. @interface VLCTransportBar ()
  14. @property (nonatomic) VLCBufferingBar *bufferingBar;
  15. @property (nonatomic) UIView *playbackPositionMarker;
  16. @property (nonatomic) UIView *scrubbingPostionMarker;
  17. @end
  18. @implementation VLCTransportBar
  19. static const CGFloat VLCTransportBarMarkerWidth = 2.0;
  20. static inline void sharedSetup(VLCTransportBar *self) {
  21. CGRect bounds = self.bounds;
  22. // Bar:
  23. VLCBufferingBar *bar = [[VLCBufferingBar alloc] initWithFrame:bounds];
  24. UIColor *barColor = [UIColor lightGrayColor];
  25. bar.bufferColor = barColor;
  26. bar.borderColor = barColor;
  27. bar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  28. bar.bufferStartFraction = self.bufferStartFraction;
  29. bar.bufferEndFraction = self.bufferEndFraction;
  30. self.bufferingBar = bar;
  31. [self addSubview:bar];
  32. // Marker:
  33. UIColor *markerColor = [UIColor whiteColor];
  34. UIView *playbackMarker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VLCTransportBarMarkerWidth, CGRectGetHeight(bounds))];
  35. playbackMarker.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  36. playbackMarker.backgroundColor = markerColor;
  37. [self addSubview:playbackMarker];
  38. self.playbackPositionMarker = playbackMarker;
  39. UIView *scrubbingMarker = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VLCTransportBarMarkerWidth, CGRectGetHeight(bounds))];
  40. [self addSubview:scrubbingMarker];
  41. scrubbingMarker.backgroundColor = markerColor;
  42. self.scrubbingPostionMarker = scrubbingMarker;
  43. // Labels:
  44. CGFloat size = [UIFont preferredFontForTextStyle:UIFontTextStyleCallout].pointSize;
  45. UIFont *font = [UIFont monospacedDigitSystemFontOfSize:size weight:UIFontWeightSemibold];
  46. UIColor *textColor = [UIColor whiteColor];
  47. UILabel *markerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  48. markerLabel.font = font;
  49. markerLabel.textColor = textColor;
  50. [self addSubview:markerLabel];
  51. self->_markerTimeLabel = markerLabel;
  52. UILabel *remainingLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  53. remainingLabel.font = font;
  54. remainingLabel.textColor = textColor;
  55. [self addSubview:remainingLabel];
  56. self->_remainingTimeLabel = remainingLabel;
  57. }
  58. - (instancetype)initWithFrame:(CGRect)frame
  59. {
  60. self = [super initWithFrame:frame];
  61. if (self) {
  62. sharedSetup(self);
  63. }
  64. return self;
  65. }
  66. - (void)awakeFromNib {
  67. [super awakeFromNib];
  68. sharedSetup(self);
  69. }
  70. - (void)setBufferStartFraction:(CGFloat)bufferStartFraction {
  71. _bufferStartFraction = bufferStartFraction;
  72. self.bufferingBar.bufferStartFraction = bufferStartFraction;
  73. }
  74. - (void)setBufferEndFraction:(CGFloat)bufferEndFraction {
  75. _bufferEndFraction = bufferEndFraction;
  76. self.bufferingBar.bufferEndFraction = bufferEndFraction;
  77. }
  78. - (void)setPlaybackFraction:(CGFloat)playbackFraction {
  79. _playbackFraction = playbackFraction;
  80. if (!self.scrubbing) {
  81. [self setScrubbingFraction:playbackFraction];
  82. }
  83. [self setNeedsLayout];
  84. }
  85. - (void)setScrubbingFraction:(CGFloat)scrubbingFraction {
  86. _scrubbingFraction = scrubbingFraction;
  87. [self setNeedsLayout];
  88. }
  89. - (void)setScrubbing:(BOOL)scrubbing {
  90. _scrubbing = scrubbing;
  91. [self setNeedsLayout];
  92. }
  93. - (void)layoutSubviews {
  94. [super layoutSubviews];
  95. const CGRect bounds = self.bounds;
  96. const CGFloat width = CGRectGetWidth(bounds)-VLCTransportBarMarkerWidth;
  97. self.playbackPositionMarker.center = CGPointMake(width*self.playbackFraction+VLCTransportBarMarkerWidth/2.0,
  98. CGRectGetMidY(bounds));
  99. const BOOL withThumbnail = NO;
  100. const CGRect scrubberFrame = scrubbingMarkerFrameForBounds_fraction_withThumb(bounds,
  101. self.scrubbingFraction,
  102. withThumbnail);
  103. self.scrubbingPostionMarker.frame = scrubberFrame;
  104. UILabel *remainingLabel = self.remainingTimeLabel;
  105. [remainingLabel sizeToFit];
  106. CGRect remainingLabelFrame = remainingLabel.frame;
  107. remainingLabelFrame.origin.y = CGRectGetMaxY(bounds)+15.0;
  108. remainingLabelFrame.origin.x = width-CGRectGetWidth(remainingLabelFrame);
  109. remainingLabel.frame = remainingLabelFrame;
  110. UILabel *markerLabel = self.markerTimeLabel;
  111. [markerLabel sizeToFit];
  112. CGPoint timeLabelCenter = remainingLabel.center;
  113. timeLabelCenter.x = self.scrubbingPostionMarker.center.x;
  114. markerLabel.center = timeLabelCenter;
  115. CGFloat remainingAlfa = CGRectIntersectsRect(markerLabel.frame, remainingLabelFrame) ? 0.0 : 1.0;
  116. remainingLabel.alpha = remainingAlfa;
  117. }
  118. static CGRect scrubbingMarkerFrameForBounds_fraction_withThumb(CGRect bounds, CGFloat fraction, BOOL withThumbnail) {
  119. const CGFloat width = CGRectGetWidth(bounds)-VLCTransportBarMarkerWidth;
  120. const CGFloat height = CGRectGetHeight(bounds);
  121. // when scrubbing marker is 4x instead of 3x bar heigt
  122. const CGFloat scrubbingHeight = height * (withThumbnail ? 4.0 : 3.0);
  123. // x position is always center of marker == view width * fraction
  124. const CGFloat scrubbingXPosition = width*fraction;
  125. CGFloat scrubbingYPosition = 0;
  126. if (withThumbnail) {
  127. // scrubbing marker bottom and bar buttom are same
  128. scrubbingYPosition = height-scrubbingHeight;
  129. } else {
  130. // scrubbing marker y center == bar y center
  131. scrubbingYPosition = height/2.0 - scrubbingHeight/2.0;
  132. }
  133. return CGRectMake(scrubbingXPosition,
  134. scrubbingYPosition,
  135. VLCTransportBarMarkerWidth,
  136. scrubbingHeight);
  137. }
  138. @end