我已经尝试过这段代码但工作不正常。在UISlider的值变化中我调用了这个方法...
@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]];
}
如果有任何想法,请帮助..
答案 0 :(得分:4)
UIColor
方法将每个组件参数放在0 to 1
范围内,因此您需要在传递滑块值之前将其除以255.0
。
答案 1 :(得分:3)
试试这个(忽略alpha滑块,这是我自己对类似任务的实际解决方案):
保持心情:让你的滑块从0变为1,以避免分裂到255.
.h文件:
IBOutlet UISlider *rSlider;
IBOutlet UISlider *gSlider;
IBOutlet UISlider *bSlider;
.m文件
-(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;
}
答案 2 :(得分:2)
使用您的设置颜色方法
-(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];
}
答案 3 :(得分:0)
对于你的Exothug
如果滑块值仅从0到1
-(IBAction)sliderValue:(UISlider*)sender
{
UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:1];
_colorLabel.backgroundColor = newColor;
}
答案 4 :(得分:0)
现在在ViewCotroller
中