0
else if (setIndexPath == 4)
{
    viewWithButtons.frame = CGRectMake(5, 40, 310, 390);

    int ch = 6;
    int cv = 6;

    for ( int i = 0 ; i < cv ; ++i )
    {
        for ( j = 0 ; j < ch ; ++j )
        {
            btnMatrix = [[[UIButton alloc] initWithFrame:CGRectMake(10+pw*j, 51+ph*i, width, height)] autorelease];
            btnMatrix.tag = i*ch+j;
            btnMatrix.userInteractionEnabled = TRUE;
            [btnMatrix addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchDown];
            [btnMatrix setBackgroundImage:imgDefaultBG forState:UIControlStateNormal];

            [viewWithButtons addSubview:btnMatrix];

            [arrButton addObject:btnMatrix];
        }
    }
}
[self.view addSubview:viewWithButtons];

我根据所选矩阵动态生成按钮,如 4 * 4、4 * 5、4 * 6、5 * 6、6 * 6 等。

按钮完全按照上面选择的矩阵设置(代码为 6 * 6 矩阵)。

现在我必须设置这些按钮的随机背景图像。

我无法设置这种动态按钮的背景。

有没有任何有效的方法可以为每个单独的按钮以随机顺序动态设置背景?

    for (intimg = 1; intimg <= 15; intimg++)
    {
        arr_FirstSet = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"%d_f_2.png",intimg], nil];
    }
    for (intimgSET2 = 1; intimgSET2 <= 15; intimgSET2++) {
        arr_SecondSet = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"%d_s_2.png",intimgSET2], nil];
    }

我正在将这种格式的图像添加到 NSMutableArray。

如何从上面使用的数组中选择随机图像。我必须匹配两个数组图像并在按钮上给出结果。如果两者都匹配,假设我按下一个按钮将显示 1_f_2.png 并按下另一个按钮将显示 1_s_2.png (作为背景)然后这两个按钮将从视图中删除。

我已经尝试了很多逻辑来得到这个。

我没有得到我感兴趣的按钮触地得分。

请建议任何代码片段或链接或代码。

提前致谢。

4

1 回答 1

1
- (UIImage*)randomImage {
    NSInteger imageCount = [[self imageArray] count]
    return [[self imageArray] objectAtIndex:arc4random() % imageCount]; 
}

如果你想要配对。使用带有两个图像的字典,或包含两个图像的简单类。

于 2012-05-29T07:03:52.767 回答