Reputation:
I have tried this code but not working properly.On value change of UISlider I called this method...
@property (strong, nonatomic) IBOutlet UISlider *r;
@property (strong, nonatomic) IBOutlet UISlider *g;
@property (strong, nonatomic) IBOutlet UISlider *b;
@property (strong, nonatomic) IBOutlet UILabel *colorLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
_r.minimumValue=0;
_r.maximumValue=255;
_g.minimumValue=0;
_g.maximumValue=255;
_b.minimumValue=0;
_b.maximumValue=255;
// Do any additional setup after loading the view, typically from a nib.
}
-(void)sliderValueChanged:(UISlider*)slider
{
[_colorLabel setBackgroundColor:[UIColor colorWithRed:_r.value green:_g.value blue:_b.value alpha:1]];
}
please help if any one have some idea..
Upvotes: 3
Views: 3026
Reputation: 79
Now in ViewCotroller
Enter code in ViewController.swift same as like Image
Upvotes: 0
Reputation: 12641
For u Exothug
If slider value from 0 to 1 only
-(IBAction)sliderValue:(UISlider*)sender
{
UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:1];
_colorLabel.backgroundColor = newColor;
}
Upvotes: 0
Reputation: 12641
Use your setting color method as
-(void)sliderValueChanged:(UISlider*)slider
{
float r=[[NSString stringWithFormat:@"%.0f",_r.value] floatValue];
float g=[[NSString stringWithFormat:@"%.0f",_g.value]floatValue];
float b=[[NSString stringWithFormat:@"%.0f",_b.value]floatValue];
UIColor *colorToSet=[UIColor colorWithRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1];
[_colorLabel setBackgroundColor:colorToSet];
}
Upvotes: 2
Reputation: 339
Try this (Ignore the alpha slider, this is my own actual solution to a similar task) :
KEEP IN MIND : let your sliders go from 0 to 1 to avoid the division through 255.
.h file :
IBOutlet UISlider *rSlider;
IBOutlet UISlider *gSlider;
IBOutlet UISlider *bSlider;
.m file
-(void)blueSlider:(id)sender
{
bSlider = (UISlider *)sender;
UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
colorView.backgroundColor = newColor;
}
-(void)greenSlider:(id)sender
{
gSlider = (UISlider *)sender;
UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
colorView.backgroundColor = newColor;
}
-(void)redSlider:(id)sender
{
rSlider = (UISlider *)sender;
UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
colorView.backgroundColor = newColor;
}
Upvotes: 3
Reputation: 119041
The UIColor
methods take each component parameter in the range 0 to 1
, so you need to divide your slider values by 255.0
before passing them.
Upvotes: 4