0

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

1 回答 1

0

方法中的参数colorWithRed:green:blue:alpha:应该在0.0to 1.0(CGFloat) 的范围内所以如果你取的值在 to 的范围内0255那么将代码更改为:

view1.backgroundColor=[UIColor colorWithRed:red1/255.0 green:green1/255.0 blue:blue1/255.0 alpha:1.0];
于 2015-06-15T12:15:08.177 回答