DivoomColorSquareView.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // DivoomColorSquareView.m
  3. // DvioomColorPicker
  4. //
  5. // Created by yanhuanpei on 2018/12/25.
  6. // Copyright © 2018 zhuk. All rights reserved.
  7. //
  8. #import "DivoomColorSquareView.h"
  9. #import "ColorIndicatorView.h"
  10. #import "HSBSupport.h"
  11. @interface DivoomColorSquareView()
  12. @property (nonatomic, strong) ColorIndicatorView *indicator;
  13. @end
  14. @implementation DivoomColorSquareView
  15. - (instancetype)init
  16. {
  17. self = [super init];
  18. if (self) {
  19. [self setupView];
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithFrame:(CGRect)frame
  24. {
  25. self = [super initWithFrame:frame];
  26. if (self) {
  27. [self setupView];
  28. }
  29. return self;
  30. }
  31. ///xib加载
  32. -(void)awakeFromNib{
  33. [super awakeFromNib];
  34. [self setupView];
  35. }
  36. - (void)setupView {
  37. [self addSubview:self.indicator];
  38. [self setHvalue:0];
  39. }
  40. -(void)layoutSubviews{
  41. [super layoutSubviews];
  42. CGFloat x = _svvalue.x * CGRectGetWidth(self.bounds);
  43. CGFloat y = CGRectGetHeight(self.bounds) - _svvalue.y * CGRectGetHeight(self.bounds);
  44. self.indicator.center = CGPointMake(x, y);
  45. //uico
  46. self.curColor = [UIColor colorWithHue:_hvalue saturation:_svvalue.x brightness:_svvalue.y alpha:1.0];
  47. // NSLog(@"layoutSubviews - %@",self.curColor);
  48. if (_colorChangedBlock) {
  49. _colorChangedBlock(self.curColor);
  50. }
  51. }
  52. - (void)updateContent {
  53. CGImageRef image = createSaturationBrightnessSquareContentImageWithHue(self.hvalue * 360);
  54. self.layer.contents = (__bridge id)image;
  55. CGImageRelease(image);
  56. }
  57. #pragma mark - 手势
  58. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  59. UITouch *touch = touches.allObjects.firstObject;
  60. [self indcatorViewWithTouch:touch];
  61. }
  62. -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  63. UITouch *touch = touches.allObjects.firstObject;
  64. [self indcatorViewWithTouch:touch];
  65. }
  66. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  67. UITouch *touch = touches.allObjects.firstObject;
  68. [self indcatorViewWithTouch:touch];
  69. if (_touchColorEndBlock) {
  70. _touchColorEndBlock();
  71. }
  72. }
  73. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  74. UITouch *touch = touches.allObjects.firstObject;
  75. [self indcatorViewWithTouch:touch];
  76. if (_touchColorEndBlock) {
  77. _touchColorEndBlock();
  78. }
  79. }
  80. - (void)indcatorViewWithTouch:(UITouch *)touch {
  81. if (touch) {
  82. CGPoint p = [touch locationInView:self];
  83. CGFloat w = CGRectGetWidth(self.bounds);
  84. CGFloat h = CGRectGetHeight(self.bounds);
  85. if (p.x < 0) {
  86. p.x = 0;
  87. }
  88. if (p.x > w) {
  89. p.x = w;
  90. }
  91. if (p.y < 0) {
  92. p.y = 0;
  93. }
  94. if (p.y > h) {
  95. p.y = h;
  96. }
  97. _svvalue = CGPointMake(p.x / w, 1.0 - p.y / h);
  98. [self setNeedsLayout];
  99. }
  100. }
  101. #pragma mark - Getter / Setter
  102. -(void)setHvalue:(CGFloat)hvalue{
  103. if (hvalue > 1) {
  104. hvalue = 1;
  105. }
  106. if (hvalue < 0) {
  107. hvalue = 0;
  108. }
  109. _hvalue = hvalue;
  110. [self updateContent];
  111. [self setNeedsLayout];
  112. }
  113. -(void)setSvvalue:(CGPoint)svvalue{
  114. if (!CGPointEqualToPoint(_svvalue, svvalue)) {
  115. _svvalue = svvalue;
  116. [self setNeedsLayout];
  117. }
  118. }
  119. - (ColorIndicatorView *)indicator{
  120. if (!_indicator) {
  121. _indicator = [[ColorIndicatorView alloc] init];
  122. }
  123. return _indicator;
  124. }
  125. @end