iOS – 拖放碰撞检测如何检测您select的项目拖到另一个子视图上?
我们正在添加拖放function,以成为一个运动场的球员。
这些职位使用界面生成器映射出来,每个都是一个单独的UIImageView。
我们希望能够将玩家的图像从屏幕侧面的长椅位置拖到场上的位置上。
如何最好地检测到正在移动的所选播放器与现有的GamePosition imageView碰撞?
我们正在寻找一种方法来检测当前位置下是否有视图或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]]; } }
检查您正在移动的项目的框架是否与您的子视图的框架相交
for (UIView *anotherView in self.subviews) { if (movingView == anotherView) continue; if (CGRectIntersectsRect(movingView.frame, anotherView.frame)) { // Do something } }
如果我是你,我会添加所有游戏相关的项目到一个NSArray和for循环通过这个。 因此,您不会检测与您的游戏无关的子视图(如标签等)的冲突。
也可能要考虑与CGRectContainsPoint()