DivoomColorBarPickerView.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // DivoomColorBarPickerView.m
  3. // DvioomColorPicker
  4. //
  5. // Created by yanhuanpei on 2018/12/25.
  6. // Copyright © 2018 zhuk. All rights reserved.
  7. //
  8. #import "DivoomColorBarPickerView.h"
  9. #import "ColorIndicatorView.h"
  10. #import "HSBSupport.h"
  11. @interface DivoomColorBarPickerView()
  12. @property (nonatomic, strong) ColorIndicatorView *indicator;
  13. @end
  14. @implementation DivoomColorBarPickerView
  15. -(void)drawRect:(CGRect)rect{
  16. float hsv[] = {0.0,1.0,1.0};
  17. CGImageRef image = createHSVBarContentImage(InfComponentIndexHue,hsv);
  18. if (image) {
  19. CGContextRef ctx = UIGraphicsGetCurrentContext();
  20. CGContextDrawImage(ctx, rect, image);
  21. CGImageRelease(image);
  22. CGFloat x = _hvalue * CGRectGetWidth(self.frame);
  23. self.indicator.center = CGPointMake(x, CGRectGetMidY(self.bounds));
  24. }
  25. }
  26. -(void)setHvalue:(CGFloat)hvalue{
  27. if (hvalue > 1) {
  28. hvalue = 1;
  29. }
  30. if (hvalue < 0) {
  31. hvalue = 0;
  32. }
  33. _hvalue = hvalue;
  34. CGFloat x = _hvalue * CGRectGetWidth(self.frame);
  35. self.indicator.center = CGPointMake(x, CGRectGetMidY(self.bounds));
  36. self.indicator.curColor = [UIColor colorWithHue:_hvalue saturation:1.0 brightness:1.0 alpha:1.0];
  37. }
  38. - (ColorIndicatorView *)indicator{
  39. if (!_indicator) {
  40. _indicator = [[ColorIndicatorView alloc] init];
  41. _indicator.curColor = [UIColor redColor]; //默认红色
  42. [self addSubview:_indicator];
  43. }
  44. return _indicator;
  45. }
  46. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  47. UITouch *touch = touches.allObjects.firstObject;
  48. [self indcatorViewWithTouch:touch];
  49. }
  50. -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  51. UITouch *touch = touches.allObjects.firstObject;
  52. [self indcatorViewWithTouch:touch];
  53. }
  54. - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  55. UITouch *touch = touches.allObjects.firstObject;
  56. [self indcatorViewWithTouch:touch];
  57. if (_touchEnddBlock) {
  58. _touchEnddBlock();
  59. }
  60. }
  61. -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  62. UITouch *touch = touches.allObjects.firstObject;
  63. [self indcatorViewWithTouch:touch];
  64. if (_touchEnddBlock) {
  65. _touchEnddBlock();
  66. }
  67. //触摸结束
  68. }
  69. - (void)indcatorViewWithTouch:(UITouch *)touch{
  70. CGPoint p = [touch locationInView:self];
  71. if (p.x > 0 && p.x <= CGRectGetWidth(self.bounds)) {
  72. self.indicator.center = CGPointMake(p.x, self.indicator.center.y);
  73. self.hvalue = self.indicator.center.x / CGRectGetWidth(self.bounds);
  74. //颜色 回调
  75. if (_ColorChangedBlock) {
  76. _ColorChangedBlock([UIColor colorWithHue:_hvalue saturation:1.0 brightness:1.0 alpha:1.0]);
  77. }
  78. NSLog(@"_hvalue - %f",_hvalue);
  79. //
  80. //hvalue 回调
  81. if (_ValueChangedBlock) {
  82. _ValueChangedBlock(_hvalue);
  83. }
  84. }
  85. }
  86. @end