TCRangeContent.m 15 KB

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