2013-01-10 40 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)) 

希望它可以帮助