色盘取出点击处的颜色组成并显示在控制器的背景上。
效果图:
下面为核心源代码。
色盘的两个类i:
// Created by dev on 16/2/4.
// Copyright © 2016年 DLS. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "DSColor.h"
@protocol DSColorDiskViewDelegate<NSObject>
@optional
-(void)selectCurrentColor:(DSColor *)currentColor;
@end
@interface DSColorDiskView : UIView
@property(nonatomic, strong)id<DSColorDiskViewDelegate>delegate;
@property(nonatomic, strong)DSColor *currentColor;
@end
//
// DSColorDiskView.m
// 色盘取色
//
// Created by dev on 16/2/4.
// Copyright © 2016年 DLS. All rights reserved.
//
#import "DSColorDiskView.h"
#import "UIView+XMGExtension.h"
#import "Masonry.h"
#define radius self.diskImageView.frame.size.height/2-10
@interface DSColorDiskView()
@property(nonatomic, strong)UIImageView *diskImageView;
@property(nonatomic, strong)UIImage *diskImage;
@property(nonatomic, strong)UIImage *pointImage;
@property(nonatomic, strong)UIImageView *pointImageView;
@property(nonatomic, assign)CGPoint pp;
@end
@implementation DSColorDiskView
-(DSColor *)currentColor{
if (!_currentColor) {
_currentColor = [[DSColor alloc] init];
}
return _currentColor;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self initView];
}
return self;
}
-(void)initView{
_diskImage = [UIImage imageNamed:@"colorPicker5"];
UIImageView *diskImageView = [[UIImageView alloc] initWithImage:_diskImage];
self.diskImageView = diskImageView;
[self addSubview:diskImageView];
self.pointImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"colorPickerKnob"]];
self.pointImageView.width = 30;
self.pointImageView.height = 30;
//self.pointImageView.backgroundColor = [UIColor redColor];
[self.diskImageView addSubview:self.pointImageView];
}
/**
* 布局时调用
*/
-(void)layoutSubviews{
CGFloat Width = self.size.width;
CGFloat Height = self.size.height;
NSLog(@"Width = %f Height = %f",Height,Width);
[self.diskImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.mas_centerY);
make.centerX.mas_equalTo(self.mas_centerX);
make.height.mas_equalTo(Height);
make.width.mas_equalTo(Width);
}];
[self.pointImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.mas_centerY);
make.centerX.mas_equalTo(self.mas_centerX);
}];
}
//可不用
-(void)setPp:(CGPoint )pp{
_pp = pp;
self.pointImageView.center = CGPointMake(_pp.x, _pp.y);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.diskImageView];
double distance = sqrt(((self.diskImageView.center.x-point.x)*(self.diskImageView.center.x -point.x)) + ((self.diskImageView.center.y - point.y)*(self.diskImageView.center.y - point.y)));
NSLog(@"radius = %f,distance = %f",radius,distance);
if (distance > radius) {
return;
}
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height), point)) {
return ;
}
//self.pp = point;
self.pointImageView.center = CGPointMake(point.x, point.y);
NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = self.diskImage.CGImage;
NSUInteger width = self.frame.size.width;
NSUInteger height = self.frame.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * 1;
NSUInteger bitsPerComponent = 8;
unsigned char pixelData[4] = { 0, 0, 0, 0 };
CGContextRef context = CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);
// Draw the pixel we are interested in onto the bitmap context
CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
CGContextRelease(context);
// Convert color values [0..255] to floats [0.0..1.0]
self.currentColor.R = (CGFloat)pixelData[0] / 255.0f;
self.currentColor.G = (CGFloat)pixelData[1] / 255.0f;
self.currentColor.B = (CGFloat)pixelData[2] / 255.0f;
NSLog(@"%f%f%f",(CGFloat)pixelData[0],(CGFloat)pixelData[1],(CGFloat)pixelData[2]);
NSLog(@"%f",self.currentColor.R);
[self.delegate selectCurrentColor:self.currentColor];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.diskImageView];
double distance = sqrt(((self.diskImageView.center.x-point.x)*(self.diskImageView.center.x -point.x)) + ((self.diskImageView.center.y - point.y)*(self.diskImageView.center.y - point.y)));
if (distance > radius) {
return;
}
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height), point)) {
return ;
}
//self.pp = point;
//中间小圆的位置
self.pointImageView.center = CGPointMake(point.x, point.y);
NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = self.diskImage.CGImage;
NSUInteger width = self.frame.size.width;
NSUInteger height = self.frame.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * 1;
NSUInteger bitsPerComponent = 8;
unsigned char pixelData[4] = { 0, 0, 0, 0 };
CGContextRef context = CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);
//
CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
CGContextRelease(context);
//
self.currentColor.R = (CGFloat)pixelData[0] / 255.0f;
self.currentColor.G = (CGFloat)pixelData[1] / 255.0f;
self.currentColor.B = (CGFloat)pixelData[2] / 255.0f;
NSLog(@"%f%f%f",(CGFloat)pixelData[0],(CGFloat)pixelData[1],(CGFloat)pixelData[2]);
NSLog(@"%f",self.currentColor.R);
[self.delegate selectCurrentColor:self.currentColor];
//不通过代理直接设置父视图的背景颜色
//self.superview.backgroundColor = [UIColor redColor];
}
@end
//
// DSColor.h
// 色盘取色
//
// Created by dev on 16/2/4.
// Copyright © 2016年 DLS. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface DSColor : NSObject
@property(nonatomic, assign)CGFloat R;
@property(nonatomic, assign)CGFloat G;
@property(nonatomic, assign)CGFloat B;
@end
//
// DSColor.m
// 色盘取色
//
// Created by dev on 16/2/4.
// Copyright © 2016年 DLS. All rights reserved.
//
#import "DSColor.h"
@implementation DSColor
@end
#import "ViewController.h"
#import "DSColorDiskView.h"
#import "Masonry.h"
@interface ViewController ()<DSColorDiskViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
DSColorDiskView *colorDiskView = [[DSColorDiskView alloc] init];
colorDiskView.delegate = self;
[self.view addSubview:colorDiskView];
//colorDiskView.backgroundColor = [UIColor redColor];
[colorDiskView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view.mas_centerX);
make.centerY.equalTo(self.view.mas_centerY).offset(-50);
make.width.mas_equalTo(200);
make.height.mas_equalTo(200);
}];
}
-(void)selectCurrentColor:(DSColor *)currentColor{
self.view.backgroundColor = [UIColor colorWithRed:currentColor.R green:currentColor.G blue:currentColor.B alpha:1.0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
源码连接地址:http://pan.baidu.com/s/1jHrn2lw