【问题标题】:UIView touch location coordinatesUIView 触摸位置坐标
【发布时间】:2010-07-30 11:27:08
【问题描述】:

UIViews 和它们对应的 superviews 中使用的坐标是什么?我有这段代码,我想检测用户可以触摸的“走廊”......类似于这张图片:alt text http://img17.imageshack.us/img17/4416/bildschirmfoto20100721u.png

这是我的代码:

    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 永远不会被调用。

谢谢

【问题讨论】:

    标签: uiview coordinates superview


    【解决方案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

    【讨论】:

    • @joec:不。半径应该只是shuttle.bounds.size.width/2。您正在计算周长。
    • 好的,所以我的视图是 200 宽,因此 outerRadius = 100,在第一个 if 中,r2shuttle 的超级视图中?非常感谢。
    • @joec:你的touchcentre 是什么?
    • 中心 = {175,200} 触摸 = {198,131}
    • @joec: dxdy 应该是 23 和 -69,因此 r2 == dx*dx+dy*dy 应该是 5290。70*70 = 4900
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    相关资源
    最近更新 更多