更改UIView背景颜色

时间:2012-07-02 19:15:25

标签: iphone objective-c uiview background rgb

我正在与委托进行一些练习,但现在我遇到了UIView的问题。这是我的故事板enter image description here

我想用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

ColorViewController.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

1 个答案:

答案 0 :(得分: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