TCVideoRangeSlider.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // TCVideoRangeSlider.m
  3. // SAVideoRangeSliderExample
  4. //
  5. // Created by annidyfeng on 2017/4/18.
  6. // Copyright © 2017年 Andrei Solovjev. All rights reserved.
  7. //
  8. #import "TCVideoRangeSlider.h"
  9. //#import "UIView+AdditionsX12.h"
  10. #import "UIView+CustomAutoLayout.h"
  11. #import "TCVideoRangeConst.h"
  12. @interface TCVideoRangeSlider()<TCRangeContentDelegate, UIScrollViewDelegate>
  13. @property BOOL disableSeek;
  14. @end
  15. @implementation TCVideoRangeSlider
  16. {
  17. BOOL _startColor;
  18. }
  19. /*
  20. // Only override drawRect: if you perform custom drawing.
  21. // An empty implementation adversely affects performance during animation.
  22. - (void)drawRect:(CGRect)rect {
  23. // Drawing code
  24. }
  25. */
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. self.bgScrollView = ({
  30. UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectZero];
  31. [self addSubview:scroll];
  32. scroll.showsVerticalScrollIndicator = NO;
  33. scroll.showsHorizontalScrollIndicator = NO;
  34. scroll.scrollsToTop = NO;
  35. scroll.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  36. scroll.delegate = self;
  37. scroll;
  38. });
  39. self.middleLine = ({
  40. UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mline.png"]];
  41. [self addSubview:imageView];
  42. imageView;
  43. });
  44. _colorInfos = [NSMutableArray array];
  45. _startColor = NO;
  46. return self;
  47. }
  48. - (void)layoutSubviews {
  49. [super layoutSubviews];
  50. self.bgScrollView.width = self.width;
  51. self.middleLine.center = self.bgScrollView.center = CGPointMake(self.width/2, self.height/2);
  52. self.middleLine.bounds = CGRectMake(0, 0, 2, self.height - 30);
  53. }
  54. - (void)setAppearanceConfig:(TCRangeContentConfig *)appearanceConfig
  55. {
  56. _appearanceConfig = appearanceConfig;
  57. }
  58. - (void)setImageList:(NSArray *)images
  59. {
  60. if (self.rangeContent) {
  61. [self.rangeContent removeFromSuperview];
  62. }
  63. if (_appearanceConfig) {
  64. self.rangeContent = [[TCRangeContent alloc] initWithImageList:images config:_appearanceConfig];
  65. } else {
  66. self.rangeContent = [[TCRangeContent alloc] initWithImageList:images];
  67. }
  68. self.rangeContent.delegate = self;
  69. [self.bgScrollView addSubview:self.rangeContent];
  70. self.bgScrollView.contentSize = [self.rangeContent intrinsicContentSize];
  71. self.bgScrollView.height = self.bgScrollView.contentSize.height;
  72. self.bgScrollView.contentInset = UIEdgeInsetsMake(0, self.width/2-self.rangeContent.pinWidth,
  73. 0, self.width/2-self.rangeContent.pinWidth);
  74. [self setCurrentPos:0];
  75. }
  76. - (void)updateImage:(UIImage *)image atIndex:(NSUInteger)index;
  77. {
  78. self.rangeContent.imageViewList[index].image = image;
  79. }
  80. - (void)setCenterPanHidden:(BOOL)isHidden
  81. {
  82. self.rangeContent.centerPin.hidden = isHidden;
  83. }
  84. - (void)setCenterPanFrame:(CGFloat)time
  85. {
  86. self.rangeContent.centerPinCenterX = time / _durationMs * self.rangeContent.width;
  87. self.rangeContent.centerPin.center = CGPointMake( self.rangeContent.centerPinCenterX, self.rangeContent.centerPin.center.y);
  88. }
  89. - (void)startColoration:(UIColor *)color alpha:(CGFloat)alpha
  90. {
  91. VideoColorInfo *info = [[VideoColorInfo alloc]init];
  92. info.colorView = [UIView new];
  93. info.colorView.backgroundColor = color;
  94. info.colorView.alpha = alpha;
  95. info.colorView.userInteractionEnabled = NO;
  96. info.startPos = _currentPos;
  97. [_colorInfos addObject:info];
  98. [self.rangeContent insertSubview:info.colorView belowSubview:self.rangeContent.leftPin];
  99. _startColor = YES;
  100. }
  101. - (void)stopColoration
  102. {
  103. VideoColorInfo *info = [_colorInfos lastObject];
  104. info.endPos = _currentPos;
  105. _startColor = NO;
  106. }
  107. - (void)removeLastColoration
  108. {
  109. VideoColorInfo *info = [_colorInfos lastObject];
  110. [info.colorView removeFromSuperview];
  111. [_colorInfos removeObject:info];
  112. }
  113. - (void)setDurationMs:(CGFloat)durationMs {
  114. _durationMs = durationMs;
  115. _leftPos = 0;
  116. _rightPos = _durationMs;
  117. [self setCurrentPos:_currentPos];
  118. _leftPos = self.durationMs * self.rangeContent.leftScale;
  119. _centerPos = self.durationMs * self.rangeContent.centerScale;
  120. _rightPos = self.durationMs * self.rangeContent.rightScale;
  121. }
  122. - (void)setCurrentPos:(CGFloat)currentPos
  123. {
  124. _currentPos = currentPos;
  125. if (_durationMs <= 0) {
  126. return;
  127. }
  128. CGFloat off = currentPos * self.rangeContent.imageListWidth / _durationMs;
  129. // off += self.rangeContent.leftPin.width;
  130. off -= self.bgScrollView.contentInset.left;
  131. self.disableSeek = YES;
  132. self.bgScrollView.contentOffset = CGPointMake(off, 0);
  133. VideoColorInfo *info = [_colorInfos lastObject];
  134. if (_startColor) {
  135. CGFloat x = 0;
  136. if (_currentPos > info.startPos) {
  137. x = self.rangeContent.pinWidth + info.startPos * self.rangeContent.imageListWidth / _durationMs;
  138. }else{
  139. x = self.rangeContent.pinWidth + _currentPos * self.rangeContent.imageListWidth / _durationMs;
  140. }
  141. CGFloat width = fabs(_currentPos - info.startPos) * self.rangeContent.imageListWidth / _durationMs;
  142. info.colorView.frame = CGRectMake(x, 0, width, self.height);
  143. }
  144. self.disableSeek = NO;
  145. }
  146. #pragma Delegate -
  147. #pragma TXVideoRangeContentDelegate
  148. - (void)onRangeLeftChanged:(TCRangeContent *)sender
  149. {
  150. _leftPos = self.durationMs * sender.leftScale;
  151. _rightPos = self.durationMs * sender.rightScale;
  152. [self.delegate onVideoRangeLeftChanged:self];
  153. }
  154. - (void)onRangeLeftChangeEnded:(TCRangeContent *)sender
  155. {
  156. _leftPos = self.durationMs * sender.leftScale;
  157. _rightPos = self.durationMs * sender.rightScale;
  158. [self.delegate onVideoRangeLeftChangeEnded:self];
  159. }
  160. - (void)onRangeCenterChanged:(TCRangeContent *)sender
  161. {
  162. _leftPos = self.durationMs * sender.leftScale;
  163. _rightPos = self.durationMs * sender.rightScale;
  164. _centerPos = self.durationMs * sender.centerScale;
  165. [self.delegate onVideoRangeCenterChanged:self];
  166. }
  167. - (void)onRangeCenterChangeEnded:(TCRangeContent *)sender
  168. {
  169. _leftPos = self.durationMs * sender.leftScale;
  170. _rightPos = self.durationMs * sender.rightScale;
  171. _centerPos = self.durationMs * sender.centerScale;
  172. [self.delegate onVideoRangeCenterChangeEnded:self];
  173. }
  174. - (void)onRangeRightChanged:(TCRangeContent *)sender
  175. {
  176. _leftPos = self.durationMs * sender.leftScale;
  177. _rightPos = self.durationMs * sender.rightScale;
  178. [self.delegate onVideoRangeRightChanged:self];
  179. }
  180. - (void)onRangeRightChangeEnded:(TCRangeContent *)sender
  181. {
  182. _leftPos = self.durationMs * sender.leftScale;
  183. _rightPos = self.durationMs * sender.rightScale;
  184. [self.delegate onVideoRangeRightChangeEnded:self];
  185. }
  186. - (void)onRangeLeftAndRightChanged:(TCRangeContent *)sender
  187. {
  188. _leftPos = self.durationMs * sender.leftScale;
  189. _rightPos = self.durationMs * sender.rightScale;
  190. }
  191. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  192. {
  193. CGFloat pos = scrollView.contentOffset.x;
  194. pos += scrollView.contentInset.left;
  195. if (pos < 0) pos = 0;
  196. if (pos > self.rangeContent.imageListWidth) pos = self.rangeContent.imageListWidth;
  197. _currentPos = self.durationMs * pos/self.rangeContent.imageListWidth;
  198. if (self.disableSeek == NO) {
  199. NSLog(@"seek %f", _currentPos);
  200. [self.delegate onVideoRange:self seekToPos:self.currentPos];
  201. }
  202. }
  203. @end