2012-07-02 137 views
1

我正在做一些與委託練習,但現在我有一個UIView的問題。這是我的故事板enter image description here更改UIView的背景顏色

我想用3個UISliders來改變UIView的顏色。 UISliders的範圍是從0到255 這是我的代碼:

ColorField是UIView的定製類

ColorField.h

#import <UIKit/UIKit.h> 

@protocol ColorFieldDelegate <NSObject> 

-(NSArray *)giveMeColors; 

@end 

@interface ColorField : UIView 

@property (nonatomic , weak) IBOutlet id<ColorFieldDelegate> delegate; 

@end 

ColorField.m

#import "ColorField.h" 

@implementation ColorField 
@synthesize delegate = _delegate; 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    NSArray *arrayOfColors = [self.delegate giveMeColors]; 
    int red = [[arrayOfColors objectAtIndex:0] intValue]; 
    int green = [[arrayOfColors objectAtIndex:1] intValue]; 
    int blue = [[arrayOfColors objectAtIndex:2] intValue]; 

    NSLog(@"Red --> %d" ,red); 
    NSLog(@"Green --> %d" ,green); 
    NSLog(@"Blue --> %d \n\n" ,blue); 

    self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0]; 

} 


@end 

ColorViewControll er.h

#import <UIKit/UIKit.h> 
#import "ColorField.h" 

@interface ColorViewController : UIViewController <ColorFieldDelegate> 

@property (nonatomic) IBOutlet ColorField *colorField; 

@property (weak, nonatomic) IBOutlet UISlider *redSlider; 

@property (weak, nonatomic) IBOutlet UISlider *greenSlider; 

@property (weak, nonatomic) IBOutlet UISlider *blueSlider; 

@end 

ColorViewController.m

#import "ColorViewController.h" 

@interface ColorViewController() 

@property (nonatomic) double redQuantity; 
@property (nonatomic) double greenQuantity; 
@property (nonatomic) double blueQuantity; 

@end 

@implementation ColorViewController 

@synthesize colorField = _colorField; 
@synthesize redSlider; 
@synthesize greenSlider; 
@synthesize blueSlider; 

@synthesize redQuantity; 
@synthesize blueQuantity; 
@synthesize greenQuantity; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.colorField setDelegate:self]; 
    [self.colorField setNeedsDisplay]; 

    self.redQuantity = 125.0; 
    self.blueQuantity = 125.0; 
    self.greenQuantity = 125.0; 

    [self.colorField setNeedsDisplay]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

-(ColorField *)colorField 
{ 
    if (_colorField == nil) { 
     _colorField = [[ColorField alloc] init]; 
    } 
    return _colorField; 
} 

- (void)viewDidUnload 
{ 
    [self setColorField:nil]; 
    [self setRedSlider:nil]; 
    [self setGreenSlider:nil]; 
    [self setBlueSlider:nil]; 

    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

-(IBAction)changeRedQuantity:(UISlider *)sender 
{ 
    //methods of UISliders 
    self.redQuantity = [sender value]; 
    [self.colorField setNeedsDisplay]; 
} 

-(IBAction)changeBlueQuantity:(UISlider *)sender 
{ 
    self.blueQuantity = [sender value]; 
    [self.colorField setNeedsDisplay]; 
} 

-(IBAction)changeGreenQuantity:(UISlider *)sender 
{ 
    self.greenQuantity = [sender value]; 
    [self.colorField setNeedsDisplay]; 
} 

-(NSArray *)giveMeColors 
{ 
    NSNumber *redNumber = [NSNumber numberWithDouble:self.redQuantity]; 
    NSNumber *greenNumber = [NSNumber numberWithDouble:self.greenQuantity]; 
    NSNumber *blueNumber = [NSNumber numberWithDouble:self.blueQuantity]; 


    NSArray *array = [[NSArray alloc] initWithObjects:redNumber, greenNumber,blueNumber, nil]; 

    return array; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

但...這就是結果:它告訴我只有紅,綠,藍三色沒有層次,例如:RGB( 255,0,75)這是

enter image description here

而不是本:

enter image description here

我不與它知道能不能告訴我...層次

謝謝!

Marco Manzoni

回答

6

您必須將滑塊值除以255.0。

[UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];

colorWithRed only accepts values 0.0-1.0