3

我想为键盘绘制 26 个按钮。有 3 行。第一行有 10 个按钮,第二行有 9 个,第三行有 7 个。从一个按钮到另一个按钮的间距是 5。每个按钮的宽度:27 和高度:40。我怎样才能同时绘制它们?

4

1 回答 1

1

您可以像这样按:- 这不是您问题的解决方案,但是..您仍然会从该代码中获得想法。-

    objDelegate.redColorArray = [[NSMutableArray alloc] initWithObjects:@"255",@"215",@"242",@"198",@"204",@"119",@"217",@"149",@"112",@"79",@"247",@"55",@"0",@"0",@"255",@"16", nil];
    objDelegate.greenColorArray = [[NSMutableArray alloc] initWithObjects:@"255",@"228",@"220",@"217",@"193",@"147",@"150",@"179",@"48",@"98",@"150",@"96",@"0",@"176",@"0",@"37", nil];
    objDelegate.blueColorArray = [[NSMutableArray alloc] initWithObjects:@"255",@"189",@"219",@"241",@"218",@"60",@"148",@"215",@"160",@"40",@"70",@"146",@"0",@"80",@"0",@"63", nil];

    UIButton *btn[16];

    buttonArray = [[NSMutableArray alloc] init];

    int x=0;
    int y=0;
   for (int i=0; i<16; i++)
    {
        btn[i] = [[UIButton alloc] initWithFrame:CGRectMake(x, y, 15, 15)];

        [btn[i] setBackgroundColor:[UIColor colorWithRed:[[objDelegate.redColorArray objectAtIndex:i]floatValue]/255.0 green:[[objDelegate.greenColorArray objectAtIndex:i]floatValue]/255.0 blue:[[objDelegate.blueColorArray objectAtIndex:i]floatValue]/255.0 alpha:1]];

        btn[i].tag = i;

        [btn[i] addTarget:self action:@selector(setColor:) forControlEvents:UIControlEventTouchUpInside];

        [buttonArray addObject:btn[i]];

        [self addSubview:btn[i]];

        x+=20;

        if((i+1)%4==0)
        {
            x=0;
            y+=20;
       }
    }

它会显示这样的按钮 按钮图片

于 2012-09-05T04:53:43.597 回答