我的UIView中有5个5x5格式的25个按钮,我将按钮标签设置为1到25,按钮标题上的数字随机出现在1-25之间。我正在尝试的是当用户点击单行,列或对角线中的5个按钮时,其所选颜色应该更改。我当前的逻辑是检查是否通过手动检查选择了行,列或对角线中的按钮标记,如button.tag 1 & 2 & 3 & 4& 5
被选中,然后[button setBackgroundImage:[UIImage imageNamed:@"buttonselectedimage.png"] forState:UIControlStateSelected];
但是我将不得不为此手动编写约12个条件。我想知道是否有任何快速公式来做这个检查。
答案 0 :(得分:1)
它有点hacky,但一种解决方案是为每个按钮分配一个不同的素数作为标记。将每个所选按钮的标记相乘。每行,每列和对角线都有一个独特的主要产品;您可以将它们存储在一个常量数组中。现在,只需检查该数组的每个元素是否是产品的一个因素;如果是,则相应的行(列,对角线)被“选中”。
例如,在3x3网格上,将它们标记为:
2 3 5
7 11 13
17 19 23
行产品为30,1001和7429.列为238,627和1495.
如果选择数字,2,3,5,11和19,则该产品为6270. 30是其中的因素,因此选择第一行。所以是627,所以也选择了该列。
如果您使用此解决方案,请对其进行评论。这不一定是显而易见的。一个更直接的解决方案是存储一个常量数组,指示哪些选定按钮组形成行,列和对角线。检查此数组的每个元素是否是当前所选按钮集的子集。这基本上是相同的解决方案(尽管代码稍重),但具有成为明显且可维护的解决方案的优势。
答案 1 :(得分:1)
#define ORDER 5 //declared at the beginning
-(void)flipButtonsVisibility:(UIButton*)sender {
[sender setSelected:!sender.isSelected];
[sender setUserInteractionEnabled:NO];
NSLog(@"tag:%ld",(long)sender.tag);
int x,y,x1,y1;
BOOL row,col,diag1,diag2;
row=FALSE;col=FALSE;diag1=FALSE;diag2=FALSE;
x=(int)sender.tag/ORDER;
y=sender.tag%ORDER;
if (!y)
{
y=ORDER;
x-=1;
}
x1=x;
y1=y;
NSString *log=@"";
NSLog(@"x=%d,y=%d",x+1,y);
//Loop to check if 5 buttons in row/col/diag are selected
//For row check
for (int i=ORDER*x+1; i<=ORDER*x+ORDER; i++)
{
log=[log stringByAppendingString:[NSString stringWithFormat:@"%d,",i]];
if (![[_buttons1_25 objectAtIndex:i-1] isSelected])
{
row=FALSE;
break;
}
NSLog(@"%d==%d",i,ORDER*x+ORDER);
if (i==ORDER*x+ORDER)
{
row=TRUE;
}
}
NSLog(@"ROW:%@",log);
log=@"";
//For column check
for (int i=0; i<ORDER; i++)
{
log=[log stringByAppendingString:[NSString stringWithFormat:@"%d,",y]];
if (![[_buttons1_25 objectAtIndex:y-1] isSelected])
{
col=FALSE;
break;
}
y+=5;
if (i+1==ORDER)
{
col=TRUE;
}
}
NSLog(@"COL:%@",log);
log=@"";
//For diagonal1 check
if (y1==x1+1)
{
for (int i=0,j=1; i<ORDER; i++,j++)
{
log=[log stringByAppendingString:[NSString stringWithFormat:@"%d,",ORDER*i+j]];
if (![[_buttons1_25 objectAtIndex:ORDER*i+j-1] isSelected])
{
diag1=FALSE;
break;
}
if (i+1==ORDER)
{
diag1=TRUE;
}
}
}
NSLog(@"D1:%@",log);
log=@"";
//For diagonal2 check
if (x1+1==ORDER-y1+1)
{
for (int i=0,j=5; i<ORDER; i++,j--)
{
log=[log stringByAppendingString:[NSString stringWithFormat:@"%d,",ORDER*i+j]];
if (![[_buttons1_25 objectAtIndex:ORDER*i+j-1] isSelected])
{
diag2=FALSE;
break;
}
if (i+1==ORDER)
{
diag2=TRUE;
}
}
}
NSLog(@"D2:%@",log);
log=@"";
//Same loops above repeated only this time we set the button colors
x=x1;
y=y1;
if (row)
{
for (int i=ORDER*x+1; i<=ORDER*x+ORDER; i++)
{
UIButton *cell=[_buttons1_25 objectAtIndex:i-1];
[cell setBackgroundImage:[UIImage imageNamed:theme.multipleButton] forState:UIControlStateSelected];
[cell setTitleColor:theme.multipleButtonTitleColor forState:UIControlStateSelected];
}
}
if (col)
{
for (int i=0; i<ORDER; i++)
{
UIButton *cell=[_buttons1_25 objectAtIndex:y-1];
[cell setBackgroundImage:[UIImage imageNamed:theme.multipleButton] forState:UIControlStateSelected];
[cell setTitleColor:theme.multipleButtonTitleColor forState:UIControlStateSelected];
y+=5;
}
}
if (diag1)
{
for (int i=0,j=1; i<ORDER; i++,j++)
{
UIButton *cell=[_buttons1_25 objectAtIndex:ORDER*i+j-1];
[cell setBackgroundImage:[UIImage imageNamed:theme.multipleButton] forState:UIControlStateSelected];
[cell setTitleColor:theme.multipleButtonTitleColor forState:UIControlStateSelected];
}
}
if (diag2)
{
for (int i=0,j=5; i<ORDER; i++,j--)
{
UIButton *cell=[_buttons1_25 objectAtIndex:ORDER*i+j-1];
[cell setBackgroundImage:[UIImage imageNamed:theme.multipleButton] forState:UIControlStateSelected];
[cell setTitleColor:theme.multipleButtonTitleColor forState:UIControlStateSelected];
}
}
}
答案 2 :(得分:0)
尝试将按钮的标签转换为(x,y):
const NSInteger rows = 5; // for you 5x5 format
const NSInteger x = tag % (rows - 1);
const NSInteger y = tag / (rows - 1);
现在,您可以轻松查看行和列。
如果所选按钮的x
等于 - 这是一行,依此类推。