DivoomColorLumpView.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // DivoomColorLumpView.m
  3. // DvioomColorPicker
  4. //
  5. // Created by yanhuanpei on 2018/12/26.
  6. // Copyright © 2018 zhuk. All rights reserved.
  7. //
  8. #import "DivoomColorLumpView.h"
  9. #import "Masonry.h"
  10. @implementation DivoomColorLumpView{
  11. NSMutableArray *_views;
  12. }
  13. - (instancetype)init
  14. {
  15. self = [super init];
  16. if (self) {
  17. [self setupView];
  18. }
  19. return self;
  20. }
  21. - (instancetype)initWithFrame:(CGRect)frame
  22. {
  23. self = [super initWithFrame:frame];
  24. if (self) {
  25. [self setupView];
  26. }
  27. return self;
  28. }
  29. - (void)setupView {
  30. _views = [[NSMutableArray alloc] init];
  31. NSArray *colors = @[[UIColor colorWithRed:1 green:1 blue:1 alpha:1],
  32. [UIColor colorWithRed:0 green:0 blue:0 alpha:1],
  33. [UIColor redColor],
  34. [UIColor yellowColor],
  35. [UIColor blueColor],
  36. [UIColor greenColor],
  37. [UIColor orangeColor]];
  38. for (int i = 0; i < colors.count; i++) {
  39. UIView *itemV = [UIView new];
  40. itemV.userInteractionEnabled = YES;
  41. itemV.layer.cornerRadius = 4;
  42. itemV.layer.masksToBounds = YES;
  43. itemV.layer.borderColor = [UIColor whiteColor].CGColor;
  44. itemV.layer.borderWidth = 1;
  45. itemV.tag = 1000 + i;
  46. [itemV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemAcion:)]];
  47. itemV.backgroundColor = colors[i];
  48. [self addSubview:itemV];
  49. [_views addObject:itemV];
  50. }
  51. }
  52. -(void)layoutSubviews{
  53. [super layoutSubviews];
  54. UIView *lastV = nil;
  55. CGFloat s = (CGRectGetWidth(self.frame) - (CGRectGetHeight(self.frame) * _views.count)) / (_views.count - 1);
  56. for (int i = 0; i < _views.count; i++) {
  57. UIView *v = _views[i];
  58. [v mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.height.width.mas_equalTo(self.mas_height);
  60. make.centerY.mas_equalTo(self);
  61. if (lastV == nil) {
  62. make.left.mas_equalTo(self);
  63. }else{
  64. make.left.mas_equalTo(lastV.mas_right).offset(s);
  65. }
  66. }];
  67. lastV = v;
  68. }
  69. }
  70. - (void)itemAcion:(UITapGestureRecognizer *)sender {
  71. if (_colorBlock) {
  72. _colorBlock([sender.view.backgroundColor copy]);
  73. }
  74. }
  75. /*
  76. // Only override drawRect: if you perform custom drawing.
  77. // An empty implementation adversely affects performance during animation.
  78. - (void)drawRect:(CGRect)rect {
  79. // Drawing code
  80. }
  81. */
  82. @end