FUColourView.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // FUColourView.m
  3. // FULiveDemo
  4. //
  5. // Created by 孙慕 on 2019/5/31.
  6. // Copyright © 2019 FaceUnity. All rights reserved.
  7. //
  8. #import "FUColourView.h"
  9. #import "FUColourFlowLayout.h"
  10. static const float cellW = 40;
  11. @interface FUColourView()<UICollectionViewDelegate,UICollectionViewDataSource>
  12. /** UICollectionView */
  13. @property (strong, nonatomic) UICollectionView *collection;
  14. @property (strong, nonatomic) NSArray <NSArray *>*colors;
  15. @end
  16. @implementation FUColourView
  17. -(instancetype)initWithFrame:(CGRect)frame{
  18. if (self = [super initWithFrame:frame]) {
  19. [self setupView];
  20. }
  21. return self;
  22. }
  23. -(void)setupView{
  24. // self.backgroundColor = [UIColor colorWithRed:5/255.0 green:15/255.0 blue:20/255.0 alpha:0.74];
  25. /* 颜色collection */
  26. FUColourFlowLayout *layout = [[FUColourFlowLayout alloc] init];
  27. _collection = [[UICollectionView alloc] initWithFrame:CGRectMake((self.frame.size.width - cellW)/2, 0, cellW, self.frame.size.height) collectionViewLayout:layout];
  28. _collection.backgroundColor = [UIColor clearColor];
  29. _collection.delegate = self;
  30. _collection.dataSource = self;
  31. _collection.showsVerticalScrollIndicator = NO;
  32. CGFloat collectionViewHeight = CGRectGetHeight(_collection.bounds);
  33. [_collection setContentInset:UIEdgeInsetsMake(collectionViewHeight/2, 0, collectionViewHeight/2,0)];
  34. [self addSubview:_collection];
  35. [_collection registerClass:[FUColourViewCell class] forCellWithReuseIdentifier:@"colourViewCell"];
  36. [self setSelCell:4];
  37. /* 中间指示圈 */
  38. CAShapeLayer *layer = [CAShapeLayer new];
  39. layer.lineWidth = 4;
  40. layer.strokeColor = [UIColor whiteColor].CGColor;
  41. layer.fillColor = [UIColor clearColor].CGColor;
  42. CGFloat radius = cellW/2 + 2;
  43. UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.collection.center radius:radius startAngle:0 endAngle:2.0f*M_PI clockwise:NO];
  44. layer.path = [path CGPath];
  45. [self.layer addSublayer:layer];
  46. }
  47. - (void)setDataColors:(NSArray <NSArray*>*)colors{
  48. _colors = colors;
  49. [_collection reloadData];
  50. }
  51. -(void)setSelCell:(int)index{
  52. float y = index * (cellW + 10) + cellW / 2 - self.collection.frame.size.height / 2;
  53. CGPoint offset = CGPointMake(0,y);
  54. [self.collection setContentOffset:offset animated:YES];
  55. }
  56. #pragma mark --- UICollectionViewDataSource
  57. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  58. return _colors.count;
  59. }
  60. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  61. FUColourViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"colourViewCell" forIndexPath:indexPath];
  62. // cell.backgroundColor = [UIColor greenColor];
  63. // NSString *imageName = self.dataArray[indexPath.row].imageName ;
  64. //
  65. // cell.topImage.image = [UIImage imageNamed:imageName] ;
  66. // cell.botlabel.text = self.dataArray[indexPath.row].title;
  67. [cell setColors:_colors[indexPath.row]];
  68. return cell;
  69. }
  70. #pragma mark --- UICollectionViewDelegateFlowLayout
  71. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  72. return CGSizeMake(cellW, cellW) ;
  73. }
  74. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  75. CGFloat collectionViewHeight = CGRectGetHeight(self.collection.frame);
  76. [collectionView setContentInset:UIEdgeInsetsMake(collectionViewHeight / 2, 0, collectionViewHeight / 2, 0)];
  77. UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
  78. CGPoint offset = CGPointMake(0, cell.center.y - collectionViewHeight / 2);
  79. [collectionView setContentOffset:offset animated:YES];
  80. if (self.delegate && [self.delegate respondsToSelector:@selector(colourViewDidSelIndex:)]) {
  81. [self.delegate colourViewDidSelIndex:(int)indexPath.row];
  82. }
  83. }
  84. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  85. // 选中\点击对应的按钮
  86. NSUInteger index = (scrollView.contentOffset.y + self.collection.frame.size.height / 2 - cellW / 2) / (cellW + 10);
  87. if (self.delegate && [self.delegate respondsToSelector:@selector(colourViewDidSelIndex:)]) {
  88. [self.delegate colourViewDidSelIndex:(int)index];
  89. }
  90. }
  91. @end
  92. @implementation FUColourViewCell
  93. - (id)initWithFrame:(CGRect)frame
  94. {
  95. self = [super initWithFrame:frame];
  96. if (self)
  97. {
  98. [self setupAllBgColorView];
  99. self.layer.cornerRadius = frame.size.width / 2;
  100. self.layer.masksToBounds = YES;
  101. }
  102. return self;
  103. }
  104. -(void)setupAllBgColorView{
  105. float h1 = self.frame.size.width;
  106. float h2 = self.frame.size.width / 2;
  107. float h3 = self.frame.size.width / 3;
  108. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0 * h2, 0 * h2, h2, h2)];
  109. view.tag = 400;
  110. [self.contentView addSubview:view];
  111. UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0 * h2, 1 * h2, h2, h2)];
  112. view1.tag = 401;
  113. [self.contentView addSubview:view1];
  114. UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(1 * h2, 0 * h2, h2, h2)];
  115. view2.tag = 402;
  116. [self.contentView addSubview:view2];
  117. UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(1 * h2, 1 * h2, h2, h2)];
  118. view3.tag = 403;
  119. [self.contentView addSubview:view3];
  120. UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(0 ,0 * h3, h1, h3)];
  121. view4.tag = 300;
  122. [self.contentView addSubview:view4];
  123. UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(0, 1 * h3, h1, h3)];
  124. view5.tag = 301;
  125. [self.contentView addSubview:view5];
  126. UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(0, 2 * h3, h1, h3)];
  127. view6.tag = 302;
  128. [self.contentView addSubview:view6];
  129. UIView *view7 = [[UIView alloc] initWithFrame:CGRectMake(0 ,0 * h2, h1, h2)];
  130. view7.tag = 200;
  131. [self.contentView addSubview:view7];
  132. UIView *view8 = [[UIView alloc] initWithFrame:CGRectMake(0, 1 * h2, h1, h2)];
  133. view8.tag = 201;
  134. [self.contentView addSubview:view8];
  135. }
  136. /* 设置背景颜色 */
  137. -(void)setColors:(NSArray *)array{
  138. for (UIView *view in self.contentView.subviews) {
  139. view.hidden = YES;
  140. }
  141. self.contentView.backgroundColor = [UIColor clearColor];
  142. NSMutableArray *mutArray = [NSMutableArray array];
  143. int colorNum = (int)array.count / 4;
  144. for (int i = 0; i < colorNum; i ++) {
  145. if([array[i * 4 + 3] intValue] == 1){
  146. [mutArray addObject:array[i * 4]];
  147. [mutArray addObject:array[i * 4 + 1]];
  148. [mutArray addObject:array[i * 4 + 2]];
  149. [mutArray addObject:array[i * 4 + 3]];
  150. }
  151. }
  152. int alphaNum = (int)mutArray.count / 4;
  153. if (alphaNum == 4) {
  154. for (int i = 0; i < 4; i ++) {
  155. float R = [array[i * 4 ] floatValue];
  156. float G = [array[i * 4 + 1] floatValue];
  157. float B = [array[i * 4 + 2] floatValue];
  158. float A = [array[i * 4 + 3] floatValue];
  159. UIView *view = [self.contentView viewWithTag:400 + i];
  160. view.hidden = NO;
  161. view.backgroundColor = [UIColor colorWithRed:R green:G blue:B alpha:A];
  162. [self.contentView bringSubviewToFront:view];
  163. }
  164. }
  165. if (alphaNum == 3) {
  166. for (int i = 0; i < 3; i ++) {
  167. float R = [array[i * 4 ] floatValue];
  168. float G = [array[i * 4 + 1] floatValue];
  169. float B = [array[i * 4 + 2] floatValue];
  170. float A = [array[i * 4 + 3] floatValue];
  171. UIView *view = [self.contentView viewWithTag:300 + i];
  172. view.hidden = NO;
  173. view.backgroundColor = [UIColor colorWithRed:R green:G blue:B alpha:A];
  174. [self.contentView bringSubviewToFront:view];
  175. }
  176. }
  177. if (alphaNum == 2) {
  178. for (int i = 0; i < 3; i ++) {
  179. float R = [array[i * 4 ] floatValue];
  180. float G = [array[i * 4 + 1] floatValue];
  181. float B = [array[i * 4 + 2] floatValue];
  182. float A = [array[i * 4 + 3] floatValue];
  183. UIView *view = [self.contentView viewWithTag:200 + i];
  184. view.hidden = NO;
  185. view.backgroundColor = [UIColor colorWithRed:R green:G blue:B alpha:A];
  186. [self.contentView bringSubviewToFront:view];
  187. }
  188. }
  189. if (alphaNum == 1) {
  190. float R = [array[0] floatValue];
  191. float G = [array[1] floatValue];
  192. float B = [array[2] floatValue];
  193. float A = [array[3] floatValue];
  194. self.contentView.backgroundColor = [UIColor colorWithRed:R green:G blue:B alpha:A];
  195. }
  196. }
  197. @end