1

我正在与委托进行一些练习,但现在我遇到了 UIView 的问题。这是我的故事板在此处输入图像描述

我想用 3 个 UISlider 改变 UIView 的颜色。UISliders 的范围是从 0 到 255。这是我的代码:

ColorField 是 UIView 自定义类

颜色字段.h

#import <UIKit/UIKit.h>

@protocol ColorFieldDelegate <NSObject>

-(NSArray *)giveMeColors;

@end

@interface ColorField : UIView

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

@end

颜色域.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

颜色视图控制器.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

颜色视图控制器.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) is THIS

在此处输入图像描述

而不是这个:

在此处输入图像描述

我不知道它不能向我显示渐变...

谢谢!!

马可·曼佐尼

4

1 回答 1

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
于 2012-07-02T19:20:26.563 回答