zoukankan      html  css  js  c++  java
  • 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
    View Code

    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
    View Code
  • 相关阅读:
    效果1时间展示隐藏
    css书写轮播图样式
    jquery案例1导航栏事件
    jquery案例三导航展示
    go并发
    效果2滑动滑入效果
    php解决导出大数据execl问题
    jquery案例3模仿京东轮播图
    jquery案例2手风琴案例
    latex自适应resize超长表格
  • 原文地址:https://www.cnblogs.com/liaods/p/4849243.html
Copyright © 2011-2022 走看看