UGCKitRangeContent.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitRangeContent.h"
  3. #import "UGCKit_UIViewAdditions.h"
  4. #import "UGCKitVideoRangeConst.h"
  5. @implementation UGCKitRangeContentConfig
  6. - (instancetype)initWithTheme:(UGCKitTheme *)theme
  7. {
  8. if (self = [super init]) {
  9. _pinWidth = PIN_WIDTH;
  10. _thumbHeight = THUMB_HEIGHT;
  11. _borderHeight = BORDER_HEIGHT;
  12. _leftPinImage = theme.editCutSliderLeftIcon;
  13. _centerPinImage = theme.editTimeEffectIndicatorIcon;
  14. _rightPigImage = theme.editCutSliderRightIcon;
  15. _indicatorImage = theme.editTimelineIndicatorIcon;
  16. _borderColor = theme.editCutSliderBorderColor;
  17. }
  18. return self;
  19. }
  20. @end
  21. @interface UGCKitRangeContent()
  22. @end
  23. @implementation UGCKitRangeContent {
  24. CGFloat _imageWidth;
  25. UGCKitRangeContentConfig* _appearanceConfig;
  26. }
  27. - (instancetype)initWithImageList:(NSArray *)images
  28. {
  29. _imageList = images;
  30. _appearanceConfig = [UGCKitRangeContentConfig new];
  31. CGRect frame = {.origin = CGPointZero, .size = [self intrinsicContentSize]};
  32. self = [super initWithFrame:frame];
  33. [self iniSubViews];
  34. return self;
  35. }
  36. - (instancetype)initWithImageList:(NSArray *)images config:(UGCKitRangeContentConfig *)config
  37. {
  38. _imageList = images;
  39. _appearanceConfig = config;
  40. CGRect frame = {.origin = CGPointZero, .size = [self intrinsicContentSize]};
  41. self = [super initWithFrame:frame];
  42. [self iniSubViews];
  43. return self;
  44. }
  45. - (void)iniSubViews
  46. {
  47. CGRect frame = self.bounds;
  48. NSMutableArray *tmpList = [NSMutableArray new];
  49. for (int i = 0; i < _imageList.count; i++) {
  50. CGRect imgFrame = CGRectMake(_appearanceConfig.pinWidth + i*[self imageWidth],
  51. _appearanceConfig.borderHeight,
  52. [self imageWidth],
  53. _appearanceConfig.thumbHeight);
  54. UIImageView *imgView = [[UIImageView alloc] initWithFrame:imgFrame];
  55. imgView.clipsToBounds = YES;
  56. imgView.image = _imageList[i];
  57. imgView.contentMode = (_imageList.count > 1 ? UIViewContentModeScaleAspectFill : UIViewContentModeScaleAspectFit);
  58. [self addSubview:imgView];
  59. [tmpList addObject:imgView];
  60. }
  61. _imageViewList = tmpList;
  62. // self.centerCover = ({
  63. // UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  64. // [self addSubview:view];
  65. // view.userInteractionEnabled = YES;
  66. // UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCenterPan:)];
  67. // [view addGestureRecognizer:panGes];
  68. // view.accessibilityIdentifier = @"center";
  69. // view;
  70. // });
  71. if (_appearanceConfig.leftCorverImage) {
  72. self.leftCover = [[UIImageView alloc] initWithImage:_appearanceConfig.leftCorverImage];
  73. self.leftCover.contentMode = UIViewContentModeCenter;
  74. self.leftCover.clipsToBounds = YES;
  75. }
  76. else {
  77. self.leftCover = [[UIImageView alloc] initWithFrame:CGRectZero];
  78. self.leftCover.backgroundColor = [UIColor blackColor];
  79. self.leftCover.alpha = 0.5;
  80. };
  81. [self addSubview:self.leftCover];
  82. if (_appearanceConfig.rightCoverImage) {
  83. self.rightCover = [[UIImageView alloc] initWithImage:_appearanceConfig.rightCoverImage];
  84. self.rightCover.contentMode = UIViewContentModeCenter;
  85. self.rightCover.clipsToBounds = YES;
  86. }
  87. else {
  88. self.rightCover = [[UIImageView alloc] initWithFrame:CGRectZero];
  89. self.rightCover.backgroundColor = [UIColor blackColor];
  90. self.rightCover.alpha = 0.5;
  91. }
  92. [self addSubview:self.rightCover];
  93. self.leftPin = ({
  94. UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.leftPinImage];
  95. imageView.contentMode = UIViewContentModeScaleToFill;
  96. imageView.ugckit_width = _appearanceConfig.pinWidth;
  97. imageView.ugckit_height = _appearanceConfig.thumbHeight + 2 * _appearanceConfig.borderHeight;
  98. [self addSubview:imageView];
  99. imageView.userInteractionEnabled = YES;
  100. UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftPan:)];
  101. [imageView addGestureRecognizer:panGes];
  102. imageView;
  103. });
  104. self.centerPin = ({
  105. UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.centerPinImage];
  106. imageView.contentMode = UIViewContentModeScaleToFill;
  107. imageView.ugckit_width = PIN_WIDTH;
  108. imageView.ugckit_height = _appearanceConfig.thumbHeight + 2 * _appearanceConfig.borderHeight;
  109. [self addSubview:imageView];
  110. imageView.userInteractionEnabled = YES;
  111. UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCenterPan:)];
  112. [imageView addGestureRecognizer:panGes];
  113. imageView;
  114. });
  115. self.centerPin.hidden = YES;
  116. self.rightPin = ({
  117. UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.rightPigImage];
  118. imageView.contentMode = UIViewContentModeScaleToFill;
  119. imageView.ugckit_width = _appearanceConfig.pinWidth;
  120. imageView.ugckit_height = _appearanceConfig.thumbHeight + 2 * _appearanceConfig.borderHeight;
  121. [self addSubview:imageView];
  122. imageView.userInteractionEnabled = YES;
  123. UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightPan:)];
  124. [imageView addGestureRecognizer:panGes];
  125. imageView;
  126. });
  127. self.topBorder = ({
  128. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  129. [self addSubview:view];
  130. view.backgroundColor = _appearanceConfig.borderColor;
  131. view;
  132. });
  133. self.bottomBorder = ({
  134. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  135. [self addSubview:view];
  136. view.backgroundColor = _appearanceConfig.borderColor;
  137. view;
  138. });
  139. _leftPinCenterX = _appearanceConfig.pinWidth / 2;
  140. _centerPinCenterX = frame.size.width / 2;
  141. _rightPinCenterX = frame.size.width- _appearanceConfig.pinWidth / 2;
  142. }
  143. - (CGSize)intrinsicContentSize {
  144. return CGSizeMake([self imageWidth] * self.imageList.count + 2 * _appearanceConfig.pinWidth, _appearanceConfig.thumbHeight + 2 * _appearanceConfig.borderHeight);
  145. }
  146. - (void)layoutSubviews {
  147. [super layoutSubviews];
  148. self.leftPin.center = CGPointMake(self.leftPinCenterX, self.ugckit_height / 2);
  149. self.centerPin.center = CGPointMake(self.centerPinCenterX, self.ugckit_height / 2);
  150. self.rightPin.center = CGPointMake(self.rightPinCenterX, self.ugckit_height / 2);
  151. [self unpdateBorder];
  152. self.centerCover.ugckit_height = _appearanceConfig.thumbHeight - 2 * _appearanceConfig.borderHeight;
  153. self.centerCover.ugckit_width = self.rightPinCenterX - self.leftPinCenterX - _appearanceConfig.pinWidth;
  154. self.centerCover.ugckit_y = _appearanceConfig.borderHeight;
  155. self.centerCover.ugckit_x = self.leftPinCenterX + _appearanceConfig.pinWidth / 2;
  156. self.leftCover.ugckit_height = _appearanceConfig.thumbHeight;
  157. self.leftCover.ugckit_width = self.leftPinCenterX;
  158. self.leftCover.ugckit_y = _appearanceConfig.borderHeight;
  159. self.leftCover.ugckit_x = 0;
  160. self.rightCover.ugckit_height = _appearanceConfig.thumbHeight;
  161. self.rightCover.ugckit_width = self.ugckit_width - self.rightPinCenterX;
  162. self.rightCover.ugckit_y = _appearanceConfig.borderHeight;
  163. self.rightCover.ugckit_x = self.rightPinCenterX;
  164. }
  165. - (void)unpdateBorder
  166. {
  167. self.topBorder.ugckit_height = _appearanceConfig.borderHeight;
  168. self.topBorder.ugckit_width = self.rightPinCenterX - self.leftPinCenterX - _appearanceConfig.pinWidth;
  169. self.topBorder.ugckit_y = 0;
  170. self.topBorder.ugckit_x = self.leftPinCenterX + _appearanceConfig.pinWidth / 2;
  171. self.bottomBorder.ugckit_height = _appearanceConfig.borderHeight;
  172. self.bottomBorder.ugckit_width = self.rightPinCenterX - self.leftPinCenterX - _appearanceConfig.pinWidth;
  173. self.bottomBorder.ugckit_y = self.leftPin.ugckit_bottom-_appearanceConfig.borderHeight;
  174. self.bottomBorder.ugckit_x = self.leftPinCenterX + _appearanceConfig.pinWidth / 2;
  175. }
  176. #pragma mark - Gestures
  177. - (void)handleLeftPan:(UIPanGestureRecognizer *)gesture
  178. {
  179. if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) {
  180. CGPoint translation = [gesture translationInView:self];
  181. _leftPinCenterX += translation.x;
  182. if (_leftPinCenterX < _appearanceConfig.pinWidth / 2) {
  183. _leftPinCenterX = _appearanceConfig.pinWidth / 2;
  184. }
  185. if (_centerPin.isHidden){
  186. if (_rightPinCenterX - _leftPinCenterX <= _appearanceConfig.pinWidth) {
  187. _leftPinCenterX = _rightPinCenterX - _appearanceConfig.pinWidth;
  188. }
  189. }else{
  190. if (_centerPinCenterX - _leftPinCenterX <= _appearanceConfig.pinWidth) {
  191. _leftPinCenterX = _centerPinCenterX - _appearanceConfig.pinWidth;
  192. }
  193. }
  194. [gesture setTranslation:CGPointZero inView:self];
  195. [self setNeedsLayout];
  196. if (gesture.state == UIGestureRecognizerStateBegan) {
  197. if ([self.delegate respondsToSelector:@selector(onRangeLeftChangeBegin:)])
  198. [self.delegate onRangeLeftChangeBegin:self];
  199. }
  200. else if (gesture.state == UIGestureRecognizerStateChanged){
  201. if ([self.delegate respondsToSelector:@selector(onRangeLeftChanged:)])
  202. [self.delegate onRangeLeftChanged:self];
  203. }
  204. else {
  205. if ([self.delegate respondsToSelector:@selector(onRangeLeftChangeEnded:)])
  206. [self.delegate onRangeLeftChangeEnded:self];
  207. }
  208. }
  209. }
  210. - (void)handleCenterPan:(UIPanGestureRecognizer *)gesture
  211. {
  212. if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) {
  213. CGPoint translation = [gesture translationInView:self];
  214. _centerPinCenterX += translation.x;
  215. if (_centerPinCenterX < _appearanceConfig.pinWidth) {
  216. _centerPinCenterX = _appearanceConfig.pinWidth;
  217. }
  218. if (_centerPinCenterX > self.ugckit_width - _appearanceConfig.pinWidth) {
  219. _centerPinCenterX = self.ugckit_width - _appearanceConfig.pinWidth;
  220. }
  221. [gesture setTranslation:CGPointZero inView:self];
  222. [self setNeedsLayout];
  223. if (gesture.state == UIGestureRecognizerStateBegan) {
  224. if ([self.delegate respondsToSelector:@selector(onRangeCenterChangeBegin:)])
  225. [self.delegate onRangeCenterChangeBegin:self];
  226. }
  227. else if (gesture.state == UIGestureRecognizerStateChanged){
  228. if ([self.delegate respondsToSelector:@selector(onRangeCenterChanged:)])
  229. [self.delegate onRangeCenterChanged:self];
  230. }
  231. else {
  232. if ([self.delegate respondsToSelector:@selector(onRangeCenterChangeEnded:)])
  233. [self.delegate onRangeCenterChangeEnded:self];
  234. }
  235. }
  236. }
  237. - (void)handleRightPan:(UIPanGestureRecognizer *)gesture
  238. {
  239. if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) {
  240. CGPoint translation = [gesture translationInView:self];
  241. _rightPinCenterX += translation.x;
  242. if (_rightPinCenterX > self.ugckit_width - _appearanceConfig.pinWidth / 2) {
  243. _rightPinCenterX = self.ugckit_width - _appearanceConfig.pinWidth / 2;
  244. }
  245. if (_centerPin.isHidden) {
  246. if (_rightPinCenterX-_leftPinCenterX <= _appearanceConfig.pinWidth) {
  247. _rightPinCenterX = _leftPinCenterX + _appearanceConfig.pinWidth;
  248. }
  249. }else{
  250. if (_rightPinCenterX-_centerPinCenterX <= _appearanceConfig.pinWidth) {
  251. _rightPinCenterX = _centerPinCenterX + _appearanceConfig.pinWidth;
  252. }
  253. }
  254. [gesture setTranslation:CGPointZero inView:self];
  255. [self setNeedsLayout];
  256. if (gesture.state == UIGestureRecognizerStateBegan) {
  257. if ([self.delegate respondsToSelector:@selector(onRangeRightChangeBegin:)])
  258. [self.delegate onRangeRightChangeBegin:self];
  259. }
  260. else if (gesture.state == UIGestureRecognizerStateChanged) {
  261. if ([self.delegate respondsToSelector:@selector(onRangeRightChanged:)])
  262. [self.delegate onRangeRightChanged:self];
  263. }
  264. else {
  265. if ([self.delegate respondsToSelector:@selector(onRangeRightChangeEnded:)])
  266. [self.delegate onRangeRightChangeEnded:self];
  267. }
  268. }
  269. }
  270. //- (void)handleCenterPan:(UIPanGestureRecognizer *)gesture
  271. //{
  272. //
  273. // if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
  274. //
  275. // CGPoint translation = [gesture translationInView:self];
  276. //
  277. // _leftPinCenterX += translation.x;
  278. // _rightPinCenterX += translation.x;
  279. //
  280. // if (_rightPinCenterX > self.width - _appearanceConfig.pinWidth || _leftPinCenterX < _appearanceConfig.pinWidth / 2){
  281. // _leftPinCenterX -= translation.x;
  282. // _rightPinCenterX -= translation.x;
  283. // }
  284. //
  285. // [gesture setTranslation:CGPointZero inView:self];
  286. //
  287. // [self setNeedsLayout];
  288. //
  289. // if ([self.delegate respondsToSelector:@selector(onRangeLeftAndRightChanged:)])
  290. // [self.delegate onRangeLeftAndRightChanged:self];
  291. //
  292. // }
  293. //}
  294. - (CGFloat)pinWidth
  295. {
  296. return _appearanceConfig.pinWidth;
  297. }
  298. - (CGFloat)imageWidth
  299. {
  300. UIImage *img = self.imageList[0];
  301. if (self.imageList.count == 1) {
  302. return MIN(img.size.width, [UIScreen mainScreen].bounds.size.width - 2 * _appearanceConfig.pinWidth);
  303. }
  304. _imageWidth = img.size.width/img.size.height*_appearanceConfig.thumbHeight;
  305. return _imageWidth;
  306. }
  307. - (CGFloat)imageListWidth {
  308. return self.imageList.count * [self imageWidth];
  309. }
  310. - (CGFloat)leftScale {
  311. CGFloat imagesLength = [self imageWidth] * self.imageViewList.count;
  312. return MAX(0, (_leftPinCenterX - _appearanceConfig.pinWidth / 2) / imagesLength);
  313. }
  314. - (CGFloat)rightScale {
  315. CGFloat imagesLength = [self imageWidth] * self.imageViewList.count;
  316. return MAX(0, (_rightPinCenterX - _appearanceConfig.pinWidth / 2 - _appearanceConfig.pinWidth) / imagesLength);
  317. }
  318. - (CGFloat)centerScale {
  319. CGFloat imagesLength = [self imageWidth] * self.imageViewList.count;
  320. return MAX(0, (_centerPinCenterX - _appearanceConfig.pinWidth / 2 - _appearanceConfig.pinWidth) / imagesLength);
  321. }
  322. @end