我想使用从步进器获得的rgb值来更改视图的背景 由于某种原因它不工作,请帮助!
int red1,green1,blue1;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return NO;
}
-(IBAction)stepper:(UIStepper *)mystep
{
if(mystep.tag==1)
{
red1=(int)mystep.value;
txt1.text=[NSString stringWithFormat:@"%d",red1];
}
if(mystep.tag==2)
{
green1=(int)mystep.value;
txt2.text=[NSString stringWithFormat:@"%d",green1];
}
if(mystep.tag==3)
{
blue1=(int)mystep.value;
txt3.text=[NSString stringWithFormat:@"%d",blue1];
}
}
-(IBAction)btnPressed
{
view1.backgroundColor=[UIColor colorWithRed:red1 green:green1 blue:blue1 alpha:1.0];
}
答案 0 :(得分:0)
colorWithRed:green:blue:alpha:
方法中的参数应在0.0
到1.0
范围内(CGFloat)
因此,如果您在0
到255
范围内取值,请将代码更改为:
view1.backgroundColor=[UIColor colorWithRed:red1/255.0 green:green1/255.0 blue:blue1/255.0 alpha:1.0];