2013-01-10 42 views
0

我得到了X和Y由屏幕上設置的UITapGestureRecognizer,獲得位置後,我發現物體接觸,所以我把條件,但不起作用。也許我把目標C中的錯誤條件? Xcode不提供任何錯誤,但該功能不起作用。邏輯運算符和錯誤的結果在目標C

-(void)tappedMapOniPad:(int)x andy:(int)y{ 
     NSLog(@"the x is: %d", x); 
     //the x is: 302 
     NSLog(@"the y is: %d", y); 
     //the y is: 37 

     if((121<x<=181) && (8<y<=51)){ //the error is here 
      self.stand = 431; 
     }else if ((181<x<=257) && (8<y<=51)){ 
      self.stand=430; 
     }else if ((257<x<=330) && (8<y<=51)){ 
      self.stand = 429; 
     } 

     NSLog(@"The stand is %d", self.stand); 
     //The stand is 431 

    } 

我該怎麼辦?

回答

4

更換

if((121<x<=181) && (8<y<=51)) 

通過

if((121 < x && x <= 181) && (8 < y && y <= 51)) 
+0

你甚至可以重新組織parethesis這方式:'if((121 Zaphod

+0

是的,我認爲OP在將前兩個條件分組在一起後有一些邏輯,所以最好保持這種方式。 –

+0

謝謝,現在它可以工作。 –

1

(121<x<=181)類型的表達式在Obj-c中無效。

使用,(x>121 && x<=181)

你完整的代碼將是這樣的:

if((x>121 && x<=181) && (y>8 && y<=51)){ //the error is here 
     self.stand = 431; 
    } 
    else if ((x>181 && x<=257) && (y>8 && y<=51)){ 
     self.stand=430; 
    } 
    else if ((x> 255 && x<=330) && (y>8 && y<=51)){ 
     self.stand = 429; 
    } 

或者你可以優化它爲:

if(y>8 && y<=51){ 
    if (x> 257 && x<=330) { 
     self.stand = 429; 
    } 
    else if(x>181){ 
     self.stand=430; 
    } 
    else if(x>121){ 
     self.stand = 431; 
    } 
} 
5
121<x<=181 

假設X:= 10 121<10<=181 - >false<=181 - >0<=181 - >真

你必須這樣做,一步一步來。

((121 < x) && (x <=181)) 

假設X:= 10 ((121 < 10) && (10 <=181)) - >false && true - >false

0

缺少&&

嘗試

if((121<x&&x <=181)&&(8<y&&y <=51)) 

希望它可以幫助