2012-02-15 79 views
0

的阵列路上我几乎新目标C和iOS开发,我需要做的是记录一个“路径”或“路径”(如点锁保护的应用程序)的应用程序。所以我想在按住按钮的同时拖动,使所有的直接邻居(右,左,上,下)。我已经开始编码了,但我不知道自己是否在正确的方向如何实现拖动按钮,以便用户不必按下每个按钮,但用手指跟踪路径目标C路线/使用按钮

viewController.m

(无效)viewDidLoad中

[super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 
for (int y=0; y < 9; y++) { 
    for (int x = 0; x < 9; x++) { 
     UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame = CGRectMake(20 + 30 * x, 20 + 30 * y, 30, 30); 
     unsigned buttonNumber = y * 9 + x + 1; 
     button.tag = buttonNumber; 

     button.backgroundColor = [UIColor blackColor]; 

     [button setTitle:[NSString stringWithFormat:@"%u", buttonNumber] forState:UIControlStateNormal]; 
     [button setTitle:[NSString stringWithFormat:@"!"] forState:UIControlStateHighlighted]; 
     [button addTarget:self action:@selector(buttonPressed:) forControlEvents:(UIControlEventTouchUpInside)]; 
     [self.view addSubview: button]; 
    } 
} 

此密码(适用于我的其他职位从豪尔赫的)生成具有独特标签的9x9的按钮网格。

和相应的操作方法,

(无效)buttonPressed:(UIButton的*)按钮

if(first){ 

    for (int y=0; y < 9; y++) { 
     for (int x = 0; x < 9; x++) { 
      unsigned buttonNumber = y * 9 + x + 1; 
      UIButton *auxButton = (UIButton *)[self.view viewWithTag:buttonNumber]; 

      if ((auxButton.tag != (button.tag + 1)) || (auxButton.tag != (button.tag - 1)) || (buttonNumber != (button.tag + 9)) || (buttonNumber != (button.tag - 9)) || (buttonNumber != button.tag)){ 

       auxButton.enabled = FALSE; 

      } 
     } 
    }else{ 
//not implemented yet 
} 

我在这里做的:首先,所有按钮将被启用,但是当第一个被压它将禁用除直接邻居之外的所有按钮(l,r,u,d)。 (条件)不适用于OR条件(||),所以所有按钮都被禁用,但是如果我只使用一个条件,比如if(auxButton.tag!= button.tag + 1),它作品。

什么能在这里是错误的? 我如何可以实现与触摸跟踪的路径按下所有经过时的按钮?

在此先感谢!

+0

我不知道你的意思是“我怎样才能实现与触摸跟踪的路径按下所有经过时的按钮?”所以,如果我的回答不是你想要的,请你给予更多的解释吗? – sch 2012-02-15 19:33:39

+0

嗨,我的意思是:按下我可以为几个按钮做什么只用一个手指按下并同时按下它是通过屏幕上移动。 – 2012-02-16 01:33:55

+0

您是否测试了我在下面提供的解决方案?尝试代码在for循环,看是否正确按钮启用/禁用当你按下一个按钮(作为一个正常的按钮)。如果它不起作用,告诉我什么是错的,也许我可以帮忙。然后尝试通过** UIControlEventTouchDragInside **,** UIControlEventTouchDragEnter **或其他类似事件更改** UIControlEventTouchUpInside **。 – sch 2012-02-16 02:16:42

回答

0

要禁用所有的按钮除了button和它的邻国,你可以做到以下几点:

for (int i = 0; i < 81; i++) { 
    UIButton *other = (UIButton *)[self.view viewWithTag:i+1]; 
    if (other != button) { 
     int deltaRow = abs((button.tag-1)%9 - (other.tag-1)%9); 
     int deltaColumn = abs((button.tag-1)/9 - (other.tag-1)/9); 
     other.enabled = (deltaRow == 0 && deltaColumn == 1) // same row, successive columns 
         ||(deltaRow == 1 && deltaColumn == 0) // same column, successive rows 
    } 
} 

,如果你想按钮太禁用取消测试(other != button)

同时检查是否要使用UIControlEventTouchDragInsideUIControlEventTouchDragEnter而不是UIControlEventTouchUpInside。请参阅文档here