UGCKitBGMSliderCutView.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitBGMSliderCutView.h"
  3. #import "UGCKit_UIViewAdditions.h"
  4. @implementation UGCKitBGMSliderCutViewConfig
  5. - (id)initWithTheme:(UGCKitTheme *)theme
  6. {
  7. if (self = [super init]) {
  8. _pinWidth = PIN_WIDTH;
  9. _thumbHeight = THUMB_HEIGHT;
  10. _borderHeight = BORDER_HEIGHT;
  11. _leftPinImage = theme.editMusicSliderLeftIcon;
  12. _rightPigImage = theme.editMusicSliderRightIcon;
  13. _borderColor = theme.editMusicSliderBorderColor;
  14. _durationUnit = 15;
  15. _labelDurationInternal = 5;
  16. }
  17. return self;
  18. }
  19. @end
  20. @interface UGCKitBGMSliderCutView()<UIScrollViewDelegate>
  21. @end
  22. @implementation UGCKitBGMSliderCutView {
  23. CGFloat _imageWidth;
  24. UGCKitBGMSliderCutViewConfig* _appearanceConfig;
  25. float sliderWidth;
  26. int dragIdx;//0 non 1 left 2 right
  27. }
  28. - (instancetype)initWithImage:(UIImage*)image config:(UGCKitBGMSliderCutViewConfig *)config
  29. {
  30. _image = image;
  31. _appearanceConfig = config;
  32. self = [super initWithFrame:_appearanceConfig.frame];
  33. [self iniSubViews];
  34. return self;
  35. }
  36. - (void)iniSubViews
  37. {
  38. CGRect frame = self.bounds;
  39. CGRect contentFrame = CGRectMake(_appearanceConfig.pinWidth,
  40. _appearanceConfig.borderHeight,
  41. _appearanceConfig.frame.size.width - 2 * _appearanceConfig.pinWidth,
  42. _appearanceConfig.thumbHeight);
  43. sliderWidth = (_appearanceConfig.frame.size.width - 2 * _appearanceConfig.pinWidth)*
  44. _appearanceConfig.duration / _appearanceConfig.durationUnit;
  45. _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,sliderWidth,_appearanceConfig.thumbHeight)];
  46. // imgView.image = _imageList[i];
  47. // imgView.contentMode = UIViewContentModeScaleToFill;
  48. UIColor *colorPattern = [[UIColor alloc] initWithPatternImage:_image];
  49. _imageView.contentMode = UIViewContentModeScaleToFill;
  50. [_imageView setBackgroundColor:colorPattern];
  51. float labelW = 40;
  52. float labelH = 10;
  53. for (float l = _appearanceConfig.labelDurationInternal; l < sliderWidth;l += _appearanceConfig.labelDurationInternal) {
  54. CGFloat lw = (l / _appearanceConfig.duration) * sliderWidth;
  55. UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(lw - labelW / 2,
  56. _appearanceConfig.borderHeight + _appearanceConfig.thumbHeight / 2 - labelH / 2,
  57. labelW,
  58. labelH)];
  59. label.backgroundColor = [UIColor colorWithWhite:1 alpha:0.5];
  60. label.layer.cornerRadius = labelH/2;
  61. label.clipsToBounds = YES;
  62. label.textAlignment = NSTextAlignmentCenter;
  63. [label setText:[UGCKitBGMSliderCutView timeString:l]];
  64. [label setTextColor:[UIColor blackColor]];
  65. [label setFont:[UIFont systemFontOfSize:10]];
  66. [_imageView addSubview:label];
  67. }
  68. _bgScrollView = [[UIScrollView alloc] initWithFrame:contentFrame];
  69. [self addSubview:_bgScrollView];
  70. _bgScrollView.showsVerticalScrollIndicator = NO;
  71. _bgScrollView.showsHorizontalScrollIndicator = NO;
  72. _bgScrollView.scrollsToTop = NO;
  73. _bgScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  74. _bgScrollView.delegate = self;
  75. _bgScrollView.contentSize = CGSizeMake(sliderWidth, _appearanceConfig.borderHeight);
  76. _bgScrollView.decelerationRate = 0.1f;
  77. _bgScrollView.bounces = NO;
  78. [_bgScrollView addSubview:_imageView];
  79. if (_appearanceConfig.leftCorverImage) {
  80. self.leftCover = [[UIImageView alloc] initWithImage:_appearanceConfig.leftCorverImage];
  81. self.leftCover.contentMode = UIViewContentModeCenter;
  82. self.leftCover.clipsToBounds = YES;
  83. }
  84. else {
  85. self.leftCover = [[UIImageView alloc] initWithFrame:CGRectZero];
  86. self.leftCover.backgroundColor = [UIColor blackColor];
  87. self.leftCover.alpha = 0.5;
  88. };
  89. [self addSubview:self.leftCover];
  90. if (_appearanceConfig.rightCoverImage) {
  91. self.rightCover = [[UIImageView alloc] initWithImage:_appearanceConfig.rightCoverImage];
  92. self.rightCover.contentMode = UIViewContentModeCenter;
  93. self.rightCover.clipsToBounds = YES;
  94. }
  95. else {
  96. self.rightCover = [[UIImageView alloc] initWithFrame:CGRectZero];
  97. self.rightCover.backgroundColor = [UIColor blackColor];
  98. self.rightCover.alpha = 0.5;
  99. }
  100. [self addSubview:self.rightCover];
  101. self.leftPin = ({
  102. UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.leftPinImage];
  103. imageView.contentMode = UIViewContentModeScaleToFill;
  104. imageView.ugckit_width = _appearanceConfig.pinWidth;
  105. [self addSubview:imageView];
  106. imageView;
  107. });
  108. self.rightPin = ({
  109. UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.rightPigImage];
  110. imageView.contentMode = UIViewContentModeScaleToFill;
  111. imageView.ugckit_width = _appearanceConfig.pinWidth;
  112. [self addSubview:imageView];
  113. imageView;
  114. });
  115. self.topBorder = ({
  116. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  117. [self addSubview:view];
  118. view.backgroundColor = _appearanceConfig.borderColor;
  119. view;
  120. });
  121. self.bottomBorder = ({
  122. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  123. [self addSubview:view];
  124. view.backgroundColor = _appearanceConfig.borderColor;
  125. view;
  126. });
  127. _leftPinCenterX = _appearanceConfig.pinWidth / 2;
  128. _rightPinCenterX = frame.size.width- _appearanceConfig.pinWidth / 2;
  129. }
  130. +(NSString*) timeString:(CGFloat) time{
  131. int t = ((int)time) % 3600;
  132. int m = t / 60;
  133. NSString* ret = nil;
  134. if(m < 10){
  135. ret = [NSString stringWithFormat:@"0%d:", m];
  136. }
  137. else ret = [NSString stringWithFormat:@"%d:", m];
  138. int s = t % 60;
  139. if(s < 10){
  140. ret = [NSString stringWithFormat:@"%@0%d", ret ,s];
  141. }
  142. else ret = [NSString stringWithFormat:@"%@%d", ret ,s];
  143. return ret;
  144. }
  145. - (CGSize)intrinsicContentSize {
  146. return CGSizeMake(_appearanceConfig.frame.size.width, _appearanceConfig.thumbHeight + 2 * _appearanceConfig.borderHeight);
  147. }
  148. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  149. {
  150. CGFloat pos = scrollView.contentOffset.x;
  151. if (pos < 0) pos = 0;
  152. if (pos > sliderWidth) pos = sliderWidth;
  153. _leftPinCenterX = pos;
  154. [self.delegate onRangeLeftChanged:self percent:pos / sliderWidth];
  155. }
  156. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
  157. if(!decelerate){
  158. CGFloat pos = scrollView.contentOffset.x;
  159. pos += scrollView.contentInset.left;
  160. if (pos < 0) pos = 0;
  161. if (pos > sliderWidth) pos = sliderWidth;
  162. [self.delegate onRangeLeftChangeEnded:self percent:pos / sliderWidth];
  163. }
  164. }
  165. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  166. CGFloat pos = scrollView.contentOffset.x;
  167. pos += scrollView.contentInset.left;
  168. if (pos < 0) pos = 0;
  169. if (pos > sliderWidth) pos = sliderWidth;
  170. NSLog(@"EndDecelerating%f",pos / sliderWidth * _appearanceConfig.duration);
  171. [self.delegate onRangeLeftChangeEnded:self percent:pos / sliderWidth];
  172. }
  173. //-(void)scrollViewWillBeginDecelerating: (UIScrollView *)scrollView
  174. //{
  175. //// [scrollView setContentOffset:scrollView.contentOffset animated:NO];
  176. //}
  177. - (void)layoutSubviews {
  178. [super layoutSubviews];
  179. self.leftPin.center = CGPointMake(self.leftPinCenterX, self.ugckit_height / 2);
  180. self.rightPin.center = CGPointMake(self.rightPinCenterX, self.ugckit_height / 2);
  181. self.topBorder.ugckit_height = _appearanceConfig.borderHeight;
  182. self.topBorder.ugckit_width = self.rightPinCenterX - self.leftPinCenterX;
  183. self.topBorder.ugckit_y = 0;
  184. self.topBorder.ugckit_x = self.leftPinCenterX;
  185. self.bottomBorder.ugckit_height = _appearanceConfig.borderHeight;
  186. self.bottomBorder.ugckit_width = self.rightPinCenterX - self.leftPinCenterX;
  187. self.bottomBorder.ugckit_y = self.leftPin.ugckit_bottom-_appearanceConfig.borderHeight;
  188. self.bottomBorder.ugckit_x = self.leftPinCenterX;
  189. self.leftCover.ugckit_height = _appearanceConfig.thumbHeight;
  190. self.leftCover.ugckit_width = self.leftPinCenterX - _appearanceConfig.pinWidth / 2;
  191. self.leftCover.ugckit_y = _appearanceConfig.borderHeight;
  192. self.leftCover.ugckit_x = _appearanceConfig.pinWidth;
  193. self.rightCover.ugckit_height = _appearanceConfig.thumbHeight;
  194. self.rightCover.ugckit_width = self.ugckit_width - self.rightPinCenterX - _appearanceConfig.pinWidth/2;
  195. self.rightCover.ugckit_y = _appearanceConfig.borderHeight;
  196. self.rightCover.ugckit_x = self.rightPinCenterX - _appearanceConfig.pinWidth/2 + 1;
  197. }
  198. -(CGFloat) getPointDistance:(CGPoint) p1 point2:(CGPoint) p2{
  199. return sqrtf((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
  200. }
  201. - (CGFloat)pinWidth
  202. {
  203. return _appearanceConfig.pinWidth;
  204. }
  205. - (CGFloat)leftScale {
  206. return _leftPinCenterX / sliderWidth;
  207. }
  208. - (CGFloat)rightScale {
  209. return (sliderWidth - _appearanceConfig.pinWidth / 2 - _appearanceConfig.pinWidth) / sliderWidth;
  210. }
  211. -(void) resetCutView{
  212. _leftPinCenterX = _appearanceConfig.pinWidth / 2;
  213. _rightPinCenterX = self.ugckit_width - _appearanceConfig.pinWidth / 2;
  214. [self setNeedsLayout];
  215. }
  216. @end