UGCKitBGMCutView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitBGMCutView.h"
  3. #import "UGCKit_UIViewAdditions.h"
  4. @implementation TCBGMCutViewConfig
  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.editCutSliderLeftIcon;
  12. _centerPinImage = theme.editCutSliderCenterIcon;
  13. _rightPigImage = theme.editCutSliderRightIcon;
  14. }
  15. return self;
  16. }
  17. @end
  18. @interface UGCKitBGMCutView()
  19. @end
  20. @implementation UGCKitBGMCutView {
  21. CGFloat _imageWidth;
  22. TCBGMCutViewConfig* _appearanceConfig;
  23. float sliderWidth;
  24. int dragIdx;//0 non 1 left 2 right
  25. }
  26. - (instancetype)initWithImageList:(NSArray *)images
  27. {
  28. _imageList = images;
  29. _appearanceConfig = [TCBGMCutViewConfig new];
  30. CGRect frame = {.origin = CGPointZero, .size = [self intrinsicContentSize]};
  31. self = [super initWithFrame:frame];
  32. [self iniSubViews];
  33. return self;
  34. }
  35. - (instancetype)initWithImageList:(NSArray *)images config:(TCBGMCutViewConfig *)config
  36. {
  37. _imageList = images;
  38. _appearanceConfig = config;
  39. self = [super initWithFrame:_appearanceConfig.frame];
  40. [self iniSubViews];
  41. return self;
  42. }
  43. - (void)iniSubViews
  44. {
  45. self.userInteractionEnabled = YES;
  46. TCPanGestureRecognizer *panGes = [[TCPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:) inview:self];
  47. [self addGestureRecognizer:panGes];
  48. CGRect frame = self.bounds;
  49. NSMutableArray *tmpList = [NSMutableArray new];
  50. for (int i = 0; i < _imageList.count; i++) {
  51. CGRect imgFrame = CGRectMake(_appearanceConfig.pinWidth + i*[self imageWidth],
  52. _appearanceConfig.borderHeight,
  53. _appearanceConfig.frame.size.width - 2 * _appearanceConfig.pinWidth,
  54. _appearanceConfig.thumbHeight);
  55. sliderWidth = _appearanceConfig.frame.size.width - 2 * _appearanceConfig.pinWidth;
  56. UIImageView *imgView = [[UIImageView alloc] initWithFrame:imgFrame];
  57. imgView.image = _imageList[i];
  58. imgView.contentMode = (_imageList.count > 1 ? UIViewContentModeScaleToFill : UIViewContentModeScaleAspectFit);
  59. imgView.contentMode = UIViewContentModeScaleToFill;
  60. [self addSubview:imgView];
  61. [tmpList addObject:imgView];
  62. }
  63. _imageViewList = tmpList;
  64. if (_appearanceConfig.leftCorverImage) {
  65. self.leftCover = [[UIImageView alloc] initWithImage:_appearanceConfig.leftCorverImage];
  66. self.leftCover.contentMode = UIViewContentModeCenter;
  67. self.leftCover.clipsToBounds = YES;
  68. }
  69. else {
  70. self.leftCover = [[UIImageView alloc] initWithFrame:CGRectZero];
  71. self.leftCover.backgroundColor = [UIColor blackColor];
  72. self.leftCover.alpha = 0.5;
  73. };
  74. [self addSubview:self.leftCover];
  75. if (_appearanceConfig.rightCoverImage) {
  76. self.rightCover = [[UIImageView alloc] initWithImage:_appearanceConfig.rightCoverImage];
  77. self.rightCover.contentMode = UIViewContentModeCenter;
  78. self.rightCover.clipsToBounds = YES;
  79. }
  80. else {
  81. self.rightCover = [[UIImageView alloc] initWithFrame:CGRectZero];
  82. self.rightCover.backgroundColor = [UIColor blackColor];
  83. self.rightCover.alpha = 0.5;
  84. }
  85. [self addSubview:self.rightCover];
  86. self.leftPin = ({
  87. UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.leftPinImage];
  88. imageView.contentMode = UIViewContentModeScaleToFill;
  89. imageView.ugckit_width = _appearanceConfig.pinWidth;
  90. [self addSubview:imageView];
  91. imageView;
  92. });
  93. self.rightPin = ({
  94. UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.rightPigImage];
  95. imageView.contentMode = UIViewContentModeScaleToFill;
  96. imageView.ugckit_width = _appearanceConfig.pinWidth;
  97. [self addSubview:imageView];
  98. imageView;
  99. });
  100. self.topBorder = ({
  101. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  102. [self addSubview:view];
  103. view.backgroundColor = [UIColor colorWithRed:0.14 green:0.80 blue:0.67 alpha:1];
  104. view;
  105. });
  106. self.bottomBorder = ({
  107. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  108. [self addSubview:view];
  109. view.backgroundColor = [UIColor colorWithRed:0.14 green:0.80 blue:0.67 alpha:1];
  110. view;
  111. });
  112. _leftPinCenterX = _appearanceConfig.pinWidth / 2;
  113. _rightPinCenterX = frame.size.width- _appearanceConfig.pinWidth / 2;
  114. }
  115. - (CGSize)intrinsicContentSize {
  116. return CGSizeMake(_appearanceConfig.frame.size.width, _appearanceConfig.thumbHeight + 2 * _appearanceConfig.borderHeight);
  117. }
  118. - (void)layoutSubviews {
  119. [super layoutSubviews];
  120. self.leftPin.center = CGPointMake(self.leftPinCenterX, self.ugckit_height / 2);
  121. self.rightPin.center = CGPointMake(self.rightPinCenterX, self.ugckit_height / 2);
  122. self.topBorder.ugckit_height = _appearanceConfig.borderHeight;
  123. self.topBorder.ugckit_width = self.rightPinCenterX - self.leftPinCenterX;
  124. self.topBorder.ugckit_y = 0;
  125. self.topBorder.ugckit_x = self.leftPinCenterX;
  126. self.bottomBorder.ugckit_height = _appearanceConfig.borderHeight;
  127. self.bottomBorder.ugckit_width = self.rightPinCenterX - self.leftPinCenterX;
  128. self.bottomBorder.ugckit_y = self.leftPin.ugckit_bottom-_appearanceConfig.borderHeight;
  129. self.bottomBorder.ugckit_x = self.leftPinCenterX;
  130. self.leftCover.ugckit_height = _appearanceConfig.thumbHeight;
  131. self.leftCover.ugckit_width = self.leftPinCenterX - _appearanceConfig.pinWidth / 2;
  132. self.leftCover.ugckit_y = _appearanceConfig.borderHeight;
  133. self.leftCover.ugckit_x = _appearanceConfig.pinWidth;
  134. self.rightCover.ugckit_height = _appearanceConfig.thumbHeight;
  135. self.rightCover.ugckit_width = self.ugckit_width - self.rightPinCenterX - _appearanceConfig.pinWidth/2;
  136. self.rightCover.ugckit_y = _appearanceConfig.borderHeight;
  137. self.rightCover.ugckit_x = self.rightPinCenterX - _appearanceConfig.pinWidth/2 + 1;
  138. }
  139. -(CGFloat) getPointDistance:(CGPoint) p1 point2:(CGPoint) p2{
  140. return sqrtf((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
  141. }
  142. #pragma mark - Gestures
  143. -(void)handleGesture:(TCPanGestureRecognizer *)gesture{
  144. if(gesture.state == UIGestureRecognizerStateBegan){
  145. CGFloat d1 = [self getPointDistance:[gesture beginPoint] point2:_leftPin.center];
  146. CGFloat d2 = [self getPointDistance:[gesture beginPoint] point2:_rightPin.center];
  147. CGFloat threshold = 50;
  148. if(d1 < threshold || d2 < threshold){
  149. if(d1 < d2){
  150. dragIdx = 1;
  151. }
  152. else dragIdx = 2;
  153. }
  154. else dragIdx = 0;
  155. }
  156. if(dragIdx == 1){
  157. [self handleLeftPan:gesture];
  158. }
  159. else if(dragIdx == 2){
  160. [self handleRightPan:gesture];
  161. }
  162. if(gesture.state == UIGestureRecognizerStateEnded){
  163. dragIdx = 0;
  164. }
  165. }
  166. - (void)handleLeftPan:(TCPanGestureRecognizer *)gesture
  167. {
  168. CGPoint translation = [gesture translationInView:self];
  169. // NSLog(@"left %f|%f", translation.x, translation.y);
  170. _leftPinCenterX += translation.x;
  171. if (_leftPinCenterX < _appearanceConfig.pinWidth / 2) {
  172. _leftPinCenterX = _appearanceConfig.pinWidth / 2;
  173. }
  174. if (_rightPinCenterX - _leftPinCenterX <= _appearanceConfig.pinWidth) {
  175. _leftPinCenterX = _rightPinCenterX - _appearanceConfig.pinWidth;
  176. }
  177. [gesture setTranslation:CGPointZero inView:[self superview]];
  178. [self setNeedsLayout];
  179. if (gesture.state == UIGestureRecognizerStateBegan) {
  180. if ([self.delegate respondsToSelector:@selector(onRangeLeftChangeBegin:)])
  181. [self.delegate onRangeLeftChangeBegin:self];
  182. }
  183. else if (gesture.state == UIGestureRecognizerStateChanged){
  184. if ([self.delegate respondsToSelector:@selector(onRangeLeftChanged:)])
  185. [self.delegate onRangeLeftChanged:self percent:0];
  186. }
  187. else {
  188. if ([self.delegate respondsToSelector:@selector(onRangeLeftChangeEnded:)])
  189. [self.delegate onRangeLeftChangeEnded:self percent:0];
  190. }
  191. }
  192. - (void)handleRightPan:(UIPanGestureRecognizer *)gesture
  193. {
  194. CGPoint translation = [gesture translationInView:self];
  195. // NSLog(@"right %f|%f", translation.x, translation.y);
  196. _rightPinCenterX += translation.x;
  197. if (_rightPinCenterX > self.ugckit_width - _appearanceConfig.pinWidth / 2) {
  198. _rightPinCenterX = self.ugckit_width - _appearanceConfig.pinWidth / 2;
  199. }
  200. if (_rightPinCenterX-_leftPinCenterX <= _appearanceConfig.pinWidth) {
  201. _rightPinCenterX = _leftPinCenterX + _appearanceConfig.pinWidth;
  202. }
  203. [gesture setTranslation:CGPointZero inView:self];
  204. [self setNeedsLayout];
  205. if (gesture.state == UIGestureRecognizerStateBegan) {
  206. if ([self.delegate respondsToSelector:@selector(onRangeRightChangeBegin:)])
  207. [self.delegate onRangeRightChangeBegin:self];
  208. }
  209. else if (gesture.state == UIGestureRecognizerStateChanged) {
  210. if ([self.delegate respondsToSelector:@selector(onRangeRightChanged:)])
  211. [self.delegate onRangeRightChanged:self];
  212. }
  213. else {
  214. if ([self.delegate respondsToSelector:@selector(onRangeRightChangeEnded:)])
  215. [self.delegate onRangeRightChangeEnded:self];
  216. }
  217. }
  218. - (CGFloat)pinWidth
  219. {
  220. return _appearanceConfig.pinWidth;
  221. }
  222. - (CGFloat)imageWidth
  223. {
  224. UIImage *img = self.imageList[0];
  225. if (self.imageList.count == 1) {
  226. return MIN(img.size.width, [UIScreen mainScreen].bounds.size.width - 2 * _appearanceConfig.pinWidth);
  227. }
  228. _imageWidth = img.size.width/img.size.height*_appearanceConfig.thumbHeight;
  229. return _imageWidth;
  230. }
  231. - (CGFloat)imageListWidth {
  232. return self.imageList.count * [self imageWidth];
  233. }
  234. - (CGFloat)leftScale {
  235. return (_leftPinCenterX - _appearanceConfig.pinWidth / 2) / sliderWidth;
  236. }
  237. - (CGFloat)rightScale {
  238. return (_rightPinCenterX - _appearanceConfig.pinWidth / 2 - _appearanceConfig.pinWidth) / sliderWidth;
  239. }
  240. -(void) resetCutView{
  241. _leftPinCenterX = _appearanceConfig.pinWidth / 2;
  242. _rightPinCenterX = self.ugckit_width - _appearanceConfig.pinWidth / 2;
  243. [self setNeedsLayout];
  244. }
  245. @end
  246. @implementation TCPanGestureRecognizer
  247. -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
  248. [super touchesBegan:touches withEvent:event];
  249. UITouch* touch = [touches anyObject];
  250. CGPoint point = [touch locationInView:_inView];
  251. _beginPoint = point;
  252. }
  253. -(instancetype)initWithTarget:(id)target action:(SEL)action inview:(UIView*)view{
  254. self= [super initWithTarget:target action:action];
  255. if(self) {
  256. _inView = view;
  257. }
  258. return self;
  259. }
  260. @end