2010-07-30 124 views
0

UIViews及其相应的超级视图中使用的坐标是什么?我有这样的代码,我想检测“走廊”,用户可以在触摸......类似这样的形象:alt text http://img17.imageshack.us/img17/4416/bildschirmfoto20100721u.pngUIView触摸位置坐标

这是我的代码有:

CGPoint touch = [recognizer locationInView:[shuttle superview]]; 
    CGPoint centre = shuttle.center; 

    int outerRadius = shuttle.bounds.size.width/2; 
    int innerRadius = (shuttle.bounds.size.width/2) - 30; 
    if ((touch.x < outerRadius && touch.y <outerRadius)){ 
     NSLog(@"in outer"); 
     if(touch.x > innerRadius && touch.y > innerRadius) { 
      NSLog(@"in corridor"); 
     } 
    } 

半径约为500和600,并且touch x和y是100和200 ...

因此,NSLog“在走廊中”永远不会被调用。

谢谢

回答

1

你的情况是错误的。根据它的走廊是一个正方形,以(0,0)为中心而不是shuttle.center。尝试

CGFloat dx = touch.x - centre.x; 
CGFloat dy = touch.y - centre.y; 
CGFloat r2 = dx*dx + dy*dy; 
if (r2 < outerRadius*outerRadius) { 
    NSLog(@"in outer"); 
    if (r2 > innerRadius*innerRadius) 
    NSLog(@"in corridor") 
} 

改为。

即使走廊确实是一个正方形,您应该检查fabs(dx), fabs(dy)而不是touch.x, touch.y

+0

我在计算半径吗? – joec 2010-07-30 12:34:39

+0

@joec:不。半径应该是“shuttle.bounds.size.width/2”。你正在计算周长。 – kennytm 2010-07-30 13:32:42

+0

好的,所以我的观点是200宽,因此outerRadius = 100,在第一个'if',r2 <100 * 100将始终为真,因为10,000总是更大。我想走廊说30点宽,所以innerRadius = 70,所以,70 * 70再次总是大于r2。这些坐标是否应该像现在一样在“班车”的超级视图中?非常感谢。 – joec 2010-07-30 14:32:59