代码之家  ›  专栏  ›  技术社区  ›  Evolve

iOS-拖放冲突检测如何检测所选项目何时拖动到其他子视图?

  •  8
  • Evolve  · 技术社区  · 16 年前

    我们正在为即将成为一个有球员位置的运动场添加拖放功能。

    使用界面生成器绘制位置,每个位置都是单独的uiImageView。

    我们希望能够将播放器图像从屏幕侧面的工作台位置拖到场地上的位置。

    当被移动的玩家与现有的游戏位置图像视图发生冲突时,我们如何才能最好地检测到?

    我们正在寻找一种方法来检测当前位置下是否有视图或ImageView。

    -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [[event allTouches] anyObject];
      CGPoint location = [touch locationInView:touch.view];
      tile1.center = location;  
    
      if gamePositionExistsAtCurrentLocation(location) { //want something like this
        [tile1 setBackgroundColor:[UIColor blueColor]]; 
      } else {
       [tile1 setBackgroundColor:[UIColor yellowColor]]; 
      }
    }
    
    2 回复  |  直到 15 年前
        1
  •  14
  •   Matthias Bauch    16 年前

    检查正在移动的项的框架是否与子视图上的框架相交。

    for (UIView *anotherView in self.subviews) {
        if (movingView == anotherView)
            continue;
        if (CGRectIntersectsRect(movingView.frame, anotherView.frame)) {
            // Do something
        }
    }
    

    如果我是你,我会将所有与游戏相关的项目添加到一个NSarray,并循环通过这个。所以你不会发现与子视图的冲突,这些子视图与你的游戏无关,比如标签等等。

        2
  •  4
  •   Srikar Appalaraju Tonetel    15 年前

    也可能想考虑 view.center 具有 CGRectContainsPoint()