iOS颜色选择器

1.导入ANImageBitmapRep库(或者文件下的ColorPickerClasses)

2.用法

FColorPickerView.h

//
//  FColorPickerView.h
//  Foowwphone
//
//  Created by Jacky-MAC on 15/7/7.
//  Copyright (c) 2015年 Fooww. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "RSColorPickerView.h"
#import "RSBrightnessSlider.h"

@interface FColorPickerView : UIViewController <RSColorPickerViewDelegate,UITextFieldDelegate>

@property (nonatomic,copy) NSString * colorCode;
@property (nonatomic,strong) UIColor * selectColor;
@property (nonatomic,copy) void (^sendColorInfo)(UIColor * sc,NSString * cc);

@end

FColorPickerView.m

//
//  FColorPickerView.m
//  Foowwphone
//
//  Created by Jacky-MAC on 15/7/7.
//  Copyright (c) 2015年 Fooww. All rights reserved.
//

#import "FColorPickerView.h"
#import "ApiConnectionDelegate.h"
#import "ProgressHUD.h"

@interface FColorPickerView ()

{
    NSInteger _upH;
    BOOL _upView;
    BOOL _shouldU;
    NSInteger _screenH;
    NSInteger _screenW;
    UIView * _oldColorV;
    UIView * _newColorV;
}
@property (nonatomic,strong) UIColor * oldColor;
@property (nonatomic,strong) RSColorPickerView * colorPicker;
@property (nonatomic,strong) RSBrightnessSlider * brightnessSlider;
@property (nonatomic,strong) UITextField * colorCodeText;

@end

@implementation FColorPickerView

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title=@"选择背景色";
    self.view.backgroundColor=grayLogColor;

    _shouldU=NO;
    _screenW=ScreenWidth;
    _screenH=ScreenHeight;
    if (isBeforeIOS7)
    {
        _upH=0;
    }else
    {
        _upH=64;
    }

    _upView=NO;

    [self createInterface];

    if (isBeforeIOS7)
    {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(15,5,22,22);
        [btn setBackgroundImage:[UIImage imageNamed:@"icon_back_left"] forState:UIControlStateNormal];
        [btn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
        UIBarButtonItem*back=[[UIBarButtonItem alloc]initWithCustomView:btn];
        self.navigationItem.leftBarButtonItem=back;
    }
}

-(void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
}

-(void)createInterface
{
    self.colorPicker = [[RSColorPickerView alloc] initWithFrame:CGRectMake(10.0, 10.0, 250.0, 250.0)];
    [self.colorPicker setDelegate:self];
    [self.colorPicker setBrightness:1.0];
    [self.colorPicker setCropToCircle:NO];
    [self.colorPicker setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:self.colorPicker];

    self.brightnessSlider = [[RSBrightnessSlider alloc] initWithFrame:CGRectMake(155.0, 120.0, 250.0, 30.0)];
    [self.brightnessSlider setColorPicker:self.colorPicker];
    [self.brightnessSlider setUseCustomSlider:NO];
    [self.view addSubview:self.brightnessSlider];

    self.colorCodeText=[[UITextField alloc]initWithFrame:CGRectMake(10, 270, 250, 30)];
    self.colorCodeText.returnKeyType = UIReturnKeyDone;
    self.colorCodeText.text=[NSString stringWithFormat:@"#%@",self.colorCode];
    self.colorCodeText.layer.borderColor=[[UIColor grayColor] CGColor];
    self.colorCodeText.layer.borderWidth=1;
    self.colorCodeText.layer.cornerRadius=4.0;
    self.colorCodeText.delegate=self;
    self.colorCodeText.tag=101;
    [self.view addSubview:self.colorCodeText];

    UIColor * oldColor=[self colorWithColorString:self.colorCode];
    _oldColorV=[[UIView alloc]initWithFrame:CGRectMake(10, 305, 120, 30)];
    _oldColorV.backgroundColor=oldColor;
    [self.view addSubview:_oldColorV];

    _newColorV=[[UIView alloc]initWithFrame:CGRectMake(140, 305, 120, 30)];
    _newColorV.backgroundColor=oldColor;
    [self.view addSubview:_newColorV];

    if (isBeforeIOS7)
    {
        UIButton *changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        changeBtn.frame = CGRectMake(ScreenWidth-50,2,40,40);
        [changeBtn setTitle:@"确定" forState:UIControlStateNormal];
        changeBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [changeBtn addTarget: self action: @selector(saveSelectColor) forControlEvents: UIControlEventTouchUpInside];
        UIBarButtonItem*changeBarBtn=[[UIBarButtonItem alloc]initWithCustomView:changeBtn];
        self.navigationItem.rightBarButtonItem=changeBarBtn;
    }else
    {
        UIBarButtonItem * changeBtn=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(saveSelectColor)];
        self.navigationItem.rightBarButtonItem = changeBtn;
    }
}
-(void)saveSelectColor
{

    self.sendColorInfo(self.selectColor,self.colorCode);
    [self goBackAction];
}

-(void)colorPickerDidChangeSelection:(RSColorPickerView *)cp
{
    if (_shouldU)
    {
        self.selectColor=[cp selectionColor];
        self.colorCode=[self getColorCodeWithColor:self.selectColor];
        self.colorCodeText.text=[NSString stringWithFormat:@"#%@",self.colorCode];
        _newColorV.backgroundColor = self.selectColor;
    }else
    {
        _shouldU=YES;
    }
}

#pragma mark-TextFieldDelegate
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self textResignFirstResponder];
}
-(void)textResignFirstResponder
{
    [self.colorCodeText resignFirstResponder];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSInteger t,h,b;
    t=textField.tag-100;
    h=270+t*35;
    b=_screenH-64-h-216;

    if (b < 0)
    {
        _upView=YES;
        self.view.frame=CGRectMake(0, b, _screenW, _screenH);
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (_upView)
    {
        self.view.frame=CGRectMake(0, _upH, _screenW, _screenH);
    }

    if ([self checkTextStringLength])
    {
        [self inputCodeChangeToColor];
    }

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
    return YES;
}

#pragma mark-ColorOperation
-(UIColor *)colorWithColorString:(NSString *)colorStrig //16进制转UIColor
{
    NSString *cString = [[colorStrig stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];

    if ([cString length] < 6)
    {
        return [UIColor whiteColor];
    }

    if ([cString hasPrefix:@"#"])
    {
        cString = [cString substringFromIndex:1];
    }

    if ([cString length] != 6)
    {
        return [UIColor whiteColor];
    }

    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];

    range.location = 2;
    NSString *gString = [cString substringWithRange:range];

    range.location = 4;
    NSString *bString = [cString substringWithRange:range];

    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    if ([self checkTextNumberWithRed:r green:g blue:b])
    {
        return [UIColor colorWithRed:((float) r / 255.0f)
                               green:((float) g / 255.0f)
                                blue:((float) b / 255.0f)
                               alpha:1.0f];
    }else
    {
        [ProgressHUD showError:@"颜色代码错误"];
        return [UIColor whiteColor];
    }
}
-(NSString *)getColorCodeWithColor:(UIColor *)color
{
    CGFloat r, g, b, a;
    [color getRed:&r green:&g blue:&b alpha:&a];
    int rgb = (int) (r * 255.0f)<<16 | (int) (g * 255.0f)<<8 | (int) (b * 255.0f)<<0;
    NSString * code = [NSString stringWithFormat:@"%06x", rgb];
    return code;
}
-(BOOL)checkTextStringLength
{
    BOOL c;
    NSString * r=self.colorCodeText.text;

    if (r.length == 7)
    {
        c=YES;
    }else
    {
        c=NO;
    }

    return c;
}
-(BOOL)checkTextNumberWithRed:(NSInteger)r green:(NSInteger)g blue:(NSInteger)b
{
    BOOL c;
    if (r>=0 && r<=255 && g>=0 && g<=255 &&b>=0 && b<=255)
    {
        c=YES;

    }else
    {
        c=NO;
    }
    return c;
}
-(void)inputCodeChangeToColor
{
    _shouldU=NO;
    self.colorCode = [self.colorCodeText.text substringFromIndex:1];
//    self.colorCode = self.colorCodeText.text;
    NSLog(@"----%@",self.colorCode);
    self.selectColor=[self colorWithColorString:self.colorCode];
    [self.colorPicker setSelectionColor:self.selectColor];
    [_newColorV setBackgroundColor:self.selectColor];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

时间: 09-30

iOS颜色选择器的相关文章

jquery颜色选择器

本站下载 第二种:纯JAVASCRIPT: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2311"> <title>DW调色板</title> <script> var ColorHex=new Array('00','33','66','99','CC','FF') var SpColo

在线颜色选择器

在做web开发的时候,有时需要自己解决配色问题,但又不知道某一种颜色对应的代码,这时在线颜色选择器就有了用武之地. 这里推荐的是我经常用的一个网站:ATOOL在线工具

2.关于QT中的Dialog(模态窗口),文件选择器,颜色选择器,字体选择器,消息提示窗口

 1 新建一个空项目 A 编写 .pro文件 QT += gui widgets HEADERS += \ MyDialog.h SOURCES += \ MyDialog.cpp B 编写MyDialog.h #ifndef MYDIALOG_H #define MYDIALOG_H #include <QDialog> class MyDialog:public QDialog { Q_OBJECT public: explicit MyDialog(QWidget *parent =

从头学Qt Quick(3)-- 用QML写一个简单的颜色选择器

先看一下效果图: 实现功能:点击不同的色块可以改变文字的颜色. 实现步骤: 一.创建一个默认的Qt Quick工程: 二.添加文件Cell.qml 这一步主要是为了实现一个自定义的组件,这个组件就是我们看到的那个色块,很明显定义成组件可以则兼UI的复用. 1 import QtQuick 2.0 2 3 Item { 4 id: container; 5 property alias cellColor: rectangle.color; 6 signal clicked(color cellC

iOS:UIPickerView选择器的使用

通过UIPickerView选择器做的一个类似于密码锁的日期时间表 源码如下: 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate> 4 @property (strong,nonatomic) NSArray *weeks;//显示第一列的数据 5 @property (strong,nonato

IOS颜色块设置

+ (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); C

iOS 开发者必不可少的75个工具

原文出处: Ben Scheirman 译文出处: Njuxjy 如果你去到一位熟练的木匠的工作室,你总是能发现他/她有一堆工具来完成不同的任务. 软件开发同样如此.你可以从软件开发者如何使用工具中看出他水准如何.有经验的开发者精于使用工具.对你目前所使用的工具不断研究,同时了解一些替代品的使用,当你目前所用的工具无法满足你的需要时可以填补空缺. 记住了这些,我将向你展示一份关于各种工具的长长的列表.其中一些我每天都用,其他的我看情况使用.如果你还有更多的工具想展示在这个列表里,可以跟我留言哦亲

0516.32款iOS开发插件和工具介绍[效率]

插件和工具介绍内容均收集于网络,太多了就不一一注明了,在此谢过! 1.Charles 为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析.Charles通过将自己设置成系统的网络访问代理服务器,使得所有的网络访问请求都通过它来完成,从而实现了网络封包的截取和分析.一个可查看所有HTTP和SSL/HTTPS流量的工具.这款工具对于你测试和服务器端进行交互的应用非常有用 2.xScope xScope带有六种不同的工具,帮助每一个设计者快速.精确的完成工作,这些工具功能灵活.强大,包括∶量

iOS开源项目周报0302

由OpenDigg 出品的iOS开源项目周报第十期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.TodayMind 在通知中心管理提醒事项 XTAudioBeacons 媒体音频数据同步到移动设备 CTPanoramaView 图片的全景触摸 YNDropDownMenu 称心如意的下拉菜单 CardSlider Tinder风格的点赞界面 SnapchatCheckbox 仿snapchat的复选框 CCF

ios 75个工具

如果你去到一位熟练的木匠的工作室,你总是能发现他/她有一堆工具来完成不同的任务. 软件开发同样如此.你可以从软件开发者如何使用工具中看出他水准如何.有经验的开发者精于使用工具.对你目前所使用的工具不断研究,同时了解一些替代品的使用,当你目前所用的工具无法满足你的需要时可以填补空缺. 记住了这些,我将向你展示一份关于各种工具的长长的列表.其中一些我每天都用,其他的我看情况使用.如果你还有更多的工具想展示在这个列表里,可以跟我留言哦亲. 我尽我所能给列表分分类.有一些是网站,有些是后台服务,但大多数