我在我的UIView中有一个5x5格式的25个按钮,我已经将按钮标签设置为1到25,按钮标题上的数字出现在1-25之间。我正在尝试的是,当用户在一行、列或对角线上单击5个按钮时,所选的颜色应该会发生变化。我目前的逻辑是检查是否通过手动检查(如button.tag 1 & 2 & 3 & 4& 5 )选择了行、形或对角线中的按钮标记,然后选择了[button setBackgroundImage:[UIImage imageNamed:@"buttonselectedimage.png"] forState:UIControlStateSelected];,但我必须手动编写大约12个条件。我想知道是否有什么快速的公式来做这个检查。
发布于 2014-03-12 04:39:12
#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];
}
}
}发布于 2013-12-05 13:51:00
这有点麻烦,但一种解决方案是为每个按钮分配一个不同的素数作为标记。将每个选定按钮的标签相乘在一起。每一行、每列和每条对角线都有一个不同的素积;您可以将它们存储在一个常量数组中。现在,只需检查该数组的每个元素是否是产品的一个因素;如果是,则相应的行(列、对角线)被“选中”。
例如,在3x3网格上,将它们标记为:
2 3 5
7 11 13
17 19 23行产品分别为30、1001和7429。列为238、627和1495。
如果选择数字2、3、5、11和19,则该产品为6270。30是其中的一个因素,因此选择了第一行。627也是,因此该列也被选中。
如果您使用此解决方案,请对其进行注释。不一定是显而易见的。一个比较简单的解决方案是存储一个常量数组,指示所选按钮的集合形成行、列和对角线。检查此数组的每个元素是否是当前选定按钮集的子集。这基本上是相同的解决方案(尽管代码稍重),但它的优点是是一个显而易见的、可维护的解决方案。
发布于 2013-12-05 13:43:36
尝试将按钮的标记转换为(x,y):
const NSInteger rows = 5; // for you 5x5 format
const NSInteger x = tag % (rows - 1);
const NSInteger y = tag / (rows - 1);现在,您可以轻松地检查行和cols。如果所选按钮的x等于-这是一行,依此类推。
https://stackoverflow.com/questions/20258770
复制相似问题