2012-02-15 78 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