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

查看:13
本文介绍了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 并通过它进行 for 循环.因此,您不会检测到与标签等与您的游戏无关的子视图的冲突.

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天全站免登陆