iphone - Change UIView's background color -


i doing exercises delegate, have problem uiview. storyboardenter image description here

i want change color of uiview 3 uisliders. range of uisliders 0 255. , code:

colorfield uiview custom class

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; }   // override drawrect: if perform custom drawing. // 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];     // additional setup after loading view, typically 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 retained subviews of 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 

but...this result: show me red, green , blue colors without gradation, example: rgb (255,0,75)

enter image description here

and not this:

enter image description here

i don't know can't show me gradation...

thanks!!

marco manzoni

you have divide slidervalues 255.0.

[uicolor colorwithred:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];

colorwithred accepts values 0.0-1.0 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -