TCBGMSliderCutView.m 9.3 KB

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