123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // DivoomColorSquareView.m
- // DvioomColorPicker
- //
- // Created by yanhuanpei on 2018/12/25.
- // Copyright © 2018 zhuk. All rights reserved.
- //
- #import "DivoomColorSquareView.h"
- #import "ColorIndicatorView.h"
- #import "HSBSupport.h"
- @interface DivoomColorSquareView()
- @property (nonatomic, strong) ColorIndicatorView *indicator;
- @end
- @implementation DivoomColorSquareView
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- [self setupView];
- }
- return self;
- }
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self setupView];
- }
- return self;
- }
- ///xib加载
- -(void)awakeFromNib{
- [super awakeFromNib];
- [self setupView];
- }
- - (void)setupView {
- [self addSubview:self.indicator];
- [self setHvalue:0];
- }
- -(void)layoutSubviews{
- [super layoutSubviews];
-
- CGFloat x = _svvalue.x * CGRectGetWidth(self.bounds);
- CGFloat y = CGRectGetHeight(self.bounds) - _svvalue.y * CGRectGetHeight(self.bounds);
- self.indicator.center = CGPointMake(x, y);
- //uico
- self.curColor = [UIColor colorWithHue:_hvalue saturation:_svvalue.x brightness:_svvalue.y alpha:1.0];
- // NSLog(@"layoutSubviews - %@",self.curColor);
- if (_colorChangedBlock) {
- _colorChangedBlock(self.curColor);
- }
- }
- - (void)updateContent {
- CGImageRef image = createSaturationBrightnessSquareContentImageWithHue(self.hvalue * 360);
- self.layer.contents = (__bridge id)image;
- CGImageRelease(image);
- }
- #pragma mark - 手势
- -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
- UITouch *touch = touches.allObjects.firstObject;
- [self indcatorViewWithTouch:touch];
- }
- -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
- UITouch *touch = touches.allObjects.firstObject;
- [self indcatorViewWithTouch:touch];
- }
- - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
- UITouch *touch = touches.allObjects.firstObject;
- [self indcatorViewWithTouch:touch];
- if (_touchColorEndBlock) {
- _touchColorEndBlock();
- }
- }
- - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
- UITouch *touch = touches.allObjects.firstObject;
- [self indcatorViewWithTouch:touch];
- if (_touchColorEndBlock) {
- _touchColorEndBlock();
- }
- }
- - (void)indcatorViewWithTouch:(UITouch *)touch {
- if (touch) {
- CGPoint p = [touch locationInView:self];
- CGFloat w = CGRectGetWidth(self.bounds);
- CGFloat h = CGRectGetHeight(self.bounds);
- if (p.x < 0) {
- p.x = 0;
- }
- if (p.x > w) {
- p.x = w;
- }
- if (p.y < 0) {
- p.y = 0;
- }
- if (p.y > h) {
- p.y = h;
- }
- _svvalue = CGPointMake(p.x / w, 1.0 - p.y / h);
- [self setNeedsLayout];
- }
- }
- #pragma mark - Getter / Setter
- -(void)setHvalue:(CGFloat)hvalue{
- if (hvalue > 1) {
- hvalue = 1;
- }
- if (hvalue < 0) {
- hvalue = 0;
- }
- _hvalue = hvalue;
- [self updateContent];
- [self setNeedsLayout];
- }
- -(void)setSvvalue:(CGPoint)svvalue{
- if (!CGPointEqualToPoint(_svvalue, svvalue)) {
- _svvalue = svvalue;
- [self setNeedsLayout];
- }
- }
- - (ColorIndicatorView *)indicator{
- if (!_indicator) {
- _indicator = [[ColorIndicatorView alloc] init];
-
- }
- return _indicator;
- }
- @end
|