iOS - 拖放碰撞检测如何检测您所选项目何时拖动到另一个子视图? [英] iOS - Drag and drop collision detection How to detect when your selected item drags over another subview?

查看:139
本文介绍了iOS - 拖放碰撞检测如何检测您所选项目何时拖动到另一个子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在添加拖放功能到什么成为一个运动场与玩家的位置。

We are adding drag and drop functionality to what is to become a sports field with positions for players.

这些位置使用Interface Builder绘制,每个都是单独的UIImageView。

The positions are mapped out using Interface Builder with each being a separate UIImageView.

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

We want to be able to drag player images from bench positions from the side of the screen onto positions on the field.

如何最好地检测何时被移动的所选播放器与现有的gamePosition imageView相撞?

How best can we detect when the selected player which is being moved around collides with an existing gamePosition imageView?

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

We are looking for a way to detect if there is a view or ImageView under the current location.

-(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]]; 
  }
}


推荐答案

如果您正在移动的项目的框架与您的子视图中的框架相交

check if the frame of the item you are moving intersects with the frame from on of your subviews

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

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

If I were you, I would add all game relevant items to an NSArray and for-loop through this. So you don't detect collisions with subviews that have nothing to do with your game like labels and so on.

这篇关于iOS - 拖放碰撞检测如何检测您所选项目何时拖动到另一个子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆