iOS,使用步进器更改视图背景颜色



我想使用从步进器获得的 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];
    }

colorWithRed:green:blue:alpha: 方法中的参数应在 0.01.0 (CGFloat) 范围内因此,如果您取的值在 0255 的范围内,然后将代码更改为:

view1.backgroundColor=[UIColor colorWithRed:red1/255.0 green:green1/255.0 blue:blue1/255.0 alpha:1.0];

最新更新